Jackson2ObjectMapperBuilderCustomizer allows us to customize part of Jackson and its ObjectMapper. However, this customization mechanism doesn't allow us to configure some advanced options of ObjectMapper, such as setDefaultSetterInfo.
We can always define a custom ObjectMapper Bean but that will disable Spring Boot's auto-configuration.
As an alternative, we can also access the ObjectMapper Bean and change it. However, would it make sense to have a more modular and composable way of customizing the ObjectMapper, similar to what was done with Jackson2ObjectMapperBuilderCustomizer but with full access to the ObjectMapper instead of pre-defined options?
Comment From: wilkinsona
You can already do this by using the auto-configured Jackson2ObjectMapperBuilder
to create your custom ObjectMapper
. Something like this:
@Bean
@Primary
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// Further customization as required
return objectMapper;
}
I am not keen on having something like an ObjectMapperCustomizer
callback in addition to the existing builder. If something is missing, I'd rather see that being added to the builder in Spring Framework. That said, if you need such a callback, you could implement it yourself and call any customisers in your own ObjectMapper
@Bean
method.
Comment From: joca-bt
Leaving this note here as it may be helpful for someone else in the future:
Spring 5.3 introduced a new customization point that allows to do this in another way (link).