How to configure Jackson in spring boot application without overriding springs default setting in pure java
You should use Jackson2ObjectMapperBuilderCustomizer
for this
@Configuration
public class JacksonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Add your customization
// jacksonObjectMapperBuilder.featuresToEnable(...)
}
};
}
}
Because a Jackson2ObjectMapperBuilderCustomizer
is a functor, Java 8 enables more compact code:
@Configuration
public class JacksonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
return builder -> {
builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Add your customization
// builder.featuresToEnable(...)
};
}
}
}
On the other hand i found this in official docu. I didn't really understood. There is no example code.
It's just saying that you only need to set the correct properties in the application.properties
file to enable or disable the various Jackson features.
spring.jackson.mapper.default-view-inclusion=false
spring.jackson.deserialization.fail-on-unknown-properties=false