This question already has an answer here:
I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored.
Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.
As stated above the annotations only works if this is specified in the parent POJO class and not the class where the conversion from JSON to Java Object is taking place.
The other alternative without touching the parent class and causing disruptions is to implement your own mapper config only for the mapper methods you need for this.
Also the package of the Deserialization feature has been moved. DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES
If using a pojo class based on JSON response. If chances are there that json changes frequently declare at pojo class level:
and at the objectMapper add this if you are converting:
So that code will not break.
In addition two 2 mechanisms already mentioned, there is also global feature that can be used to suppress all failures caused by unknown (unmapped) properties:
This is the default used in absence of annotations, and can be convenient fallback.
Make sure that you place the
@JsonIgnoreProperties(ignoreUnknown = true)
annotation to the parent POJO class which you want to populate as a result of parsing the JSON response and not the class where the conversion from JSON to Java Object is taking place.Starting with Jackson version 2.4 and above there have been some changes. Here is how you do it now:
..........................................................................
For more information see the 10 minutes Configuration tutorial at:
https://github.com/FasterXML/jackson-databind@JsonIgnoreProperties(ignoreUnknown = true)
worked well for me. I have a java application which runs on tomcat with jdk 1.7.