Force Jackson to add addional wrapping using annotations

前端 未结 7 1024
渐次进展
渐次进展 2021-02-12 20:41

I have the following class:

public class Message {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(St         


        
7条回答
  •  伪装坚强ぢ
    2021-02-12 21:29

    With Jackson 2.x use can use the following to enable wrapper without adding addition properties in the ObjectMapper

    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.annotation.JsonTypeName;
    
    @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
    @JsonTypeName(value = "student")
    public class Student {
      private String name;
      private String id;
    }
    

提交回复
热议问题