How to map JSON fields to custom object properties? [duplicate]

倖福魔咒の 提交于 2019-12-04 03:09:11

To map a JSON property to a java object with a different name use @JsonProperty annotation, and your code will be :

public class JsonEntity {
    @JsonProperty(value="message")
    private String value;
}

Try this:

@JsonProperty("message")
private String value;

In case you familiar it, you can also use Jaxb annotations to marshal/unmarshal json using Jackson

@XmlRootElement
public class JsonEntity {
   @XmlElement(name = "message")
  private String value;
}

But you must initialize your Jackson context propery. Here an example how to initialize Jackson context with Jaxb annotations.

ObjectMapper mapper = new ObjectMapper();

AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!