Skip to content
Advertisement

Jackson throws error mapping exception when I try to deserialize a list of objects

I try to deserialize a huge API payload. This payload contains way more fields than I need and therefore I am utilizing @JsonIgnoreProperties(ignoreUnknown = true). However at some point the deserialization fails with the error message:

JavaScript

I found solutions for that case that suggested the use of

JavaScript

I tried this. But it did not help. Also my result data is not a single valued array. It actually contains two values – therefore the solution doesn’t add up anyway.

Here are my classes that are the target of the deserialization.

JavaScript
JavaScript

Here is the unit test where I test it out:

JavaScript

And here is the payload I want to deserialize

JavaScript

Advertisement

Answer

You should simply annotate the parameters of your @JsonCreators.

JavaScript

becomes

JavaScript

and same goes for the other constructor.

Explanation:

  • Serialization: you have an instance with fields, you annotate a field with @JsonProperty("name") (or the getter with @JsonValue) and that allows Jackson to build a string json from your instance by reflection.
  • Deserialization: following the same logic, when you annotate a constructor with @JsonCreator you’re telling Jackson this is the constructor they should use to build your object from the Json string they have. However, either you give them an empty constructor (and then by reflection they will set each field later), or you have to tell them which field of the Json string they have to use inside each constructor parameter.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement