7

I'm using spring-boot and want to customize the ObjectMapper created.

What I want to do is be able to serialize objects that do not have a getter or setters. Before this could be done by putting JsonAutoDetect.Visibility.ANY on the ObjectMapper.

But how can I enable this feature using the Jackson2ObjectMapperBuilder bean I'm currently exposing ?

Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60

3 Answers3

9

You can use a Jackson2ObjectMapperBuilder subclass that overrides the configure(ObjectMapper) method:

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    return new Jackson2ObjectMapperBuilder() {

        @Override
        public void configure(ObjectMapper objectMapper) {
            super.configure(objectMapper);
            objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        }

    };

}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • app is failing to start with boot 1.4.0 and this `Caused by: java.lang.NullPointerException: null at org.springframework.data.rest.core.UriToEntityConverter.(UriToEntityConverter.java:71) ~[spring-data-rest-core-2.5.2.RELEASE.jar:na]` – xenoterracide Aug 17 '16 at 04:08
  • looking at the line before that... `PersistentEntity, ?> entity = entities.getPersistentEntity(rawType);` where `rawType` is `JsonDeserializer`... – xenoterracide Aug 17 '16 at 04:15
  • it seems to only happen as a result of defining `Jackson2ObjectMapperBuilder` as a subclass – xenoterracide Aug 17 '16 at 04:28
  • This method will disable any `Jackson2ObjectMapperBuilderCustomizer` beans that are applied in the original `Jackson2ObjectMapperBuilder` bean method (_JacksonAutoConfiguration.java_) – cdalxndr Apr 07 '19 at 17:17
8

If you want to keep the ObjectMapper configurable through the spring.jackson.* properties that Spring Boot provides, then you better don't define your own Jackson2ObjectMapperBuilder bean (check JacksonObjectMapperBuilderConfiguration inside JacksonAutoConfiguration class for details).

What you can do instead is this:

@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder mapperBuilder) {
    return mapperBuilder.build().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
}
Rodrigo Quesada
  • 1,460
  • 1
  • 14
  • 20
  • I had to exclude HypermediaAutoConfiguration for this solution to work, see [SpringFox dependency breaking my Spring Context](http://stackoverflow.com/questions/31307883/springfox-dependency-breaking-my-spring-context/32281734#32281734) – 0xced Sep 15 '15 at 22:02
1

I spend a half of day to play with different settings. So I manage to work it (1.3.2.RELEASE) when:

  • I configure jackson in simple @Configuration annotated config class (not extended from WebMvcConfigurerAdapter)
  • I have NOT@EnableWebMvc

Then Jackson2ObjectMapperBuilder objectMapperBuilder solution is work, but spring.jackson.serialization.indent_output: true in properties ignored.

At last I finished with

 @Autowired(required = true)
 public void configeJackson(ObjectMapper objectMapper) {
     objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
            .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
 }

But all this is puzzle for me. I wrote a question about any explanation of all this magic in order to have some undestanding and solve problem not by trial-and-error method: Are there any Spring Boot documentation for understanding how web mvc configuration is work?

Community
  • 1
  • 1
Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197