Force Jackson to add addional wrapping using annotations

前端 未结 7 978
渐次进展
渐次进展 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:46

    On workaround: you don't absolutely need those getters/setters, so could just have:

    public class MessageWrapper {
      public Message message;
    }
    

    or perhaps add convenience constructor:

    public class MessageWrapper {
      public Message message;
      @JsonCreator
      public MessageWrapper(@JsonProperty("message") Message m) { 
           message = m; 
      }
    }
    

    There is a way to add wrapping too; with 1.9 you can use SerializationConfig.Feature.WRAP_ROOT_ELEMENT and DeserializationConfig.Feature.UNWRAP_ROOT_ELEMENT. And if you want to change the wrapper name (by default it is simply unqualified class name), you can use @JsonRootName annotation

    Jackson 2.0 adds further dynamic options via ObjectReader and ObjectWriter, as well as JAX-RS annotations.

    0 讨论(0)
提交回复
热议问题