Jackson not populating all properties

后端 未结 1 943
情话喂你
情话喂你 2021-01-11 23:34

I am working on a simple example using Jackson library to convert a json string back to Java object but I see only few properties are

相关标签:
1条回答
  • 2021-01-12 00:11

    You said in your comment, that you're using Jackson in version "2.5.4", but you're importing the ObjectMapper class from the org.codehaus package. This means, that this class is from version 1.9.13 (or from an older version).

    I can reproduce your problem if I mix the versions using ObjectMapper and JsonIgnoreProperties from version 1.9.13 (org.codehaus) and JsonProperty from version 2.6.0 (com.fasterxml).

    Output:

    Sample [prop1=null, prop2=2, prop3=null, prop4=4]

    If I only use version 1.9.13 or 2.6.0, then the result is ok:

    Sample [prop1=1, prop2=2, prop3=3, prop4=4]

    (for both)

    So I recommend to make sure that you don't mix the used libraries and I recommend to use the newest version, which is from FasterXML. But the used version is up to you.

    You can download the jar file from here:

    • org.codehaus.jackson v1.9.13

    • com.fasterxml.jackson (core) v2.6.0

    Btw about your comment:

    @OldCurmudgeon, Thanks for responding. Changing the fields to public has not fixed the issue. I have removed the @JsonProperty annotation and then changed the setter methods to setProp_1 & setProp_3, it worked. So does it mean that there is an issue with @JsonProperty annotation?

    Yes, you have (or hopefully had :P) a problem with that annotation: it was from a different Jackson version.

    About your edit:

    The link to the Jackson lib from fasterXML in the maven repository has one big advantage: it shows you which lib you should download to work with Jackson in your project.

    You need:

    • Jackson Databind (which also has the ObjectMapper class)
    • Jackson Core
    • Jackson Annotations
    0 讨论(0)
提交回复
热议问题