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.
Up to date and complete answer with Jackson 2
Using Annotation
See JsonIgnoreProperties on Jackson online documentation.
Using Configuration
Less intrusive than annotation.
See FAIL_ON_UNKNOWN_PROPERTIES on Jackson online documentation.
For whom are using Spring Boot, you can configure the default behaviour of Jackson by using the
Jackson2ObjectMapperBuilder
.For example :
Then you can autowire the
ObjectMapper
everywhere you need it (by default, this object mapper will also be used for http content conversion).I'm using jackson-xxx 2.8.5.Maven Dependency like:
First,If you want ignore unknown properties globally.you can config
ObjectMapper
.Like below:
If you want ignore some class,you can add annotation
@JsonIgnoreProperties(ignoreUnknown = true)
on your class like:Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).
Add the following to the top of your class (not to individual methods):
Depending on the jackson version you are using you would have to use a different import in the current version it is:
in older versions it has been:
You can annotate the specific property in your POJO with @JsonIgnore.
it can be achieved 2 ways:
Mark the POJO to ignore unknown properties
Configure ObjectMapper that serializes/De-serializes the POJO/json as below: