Spring Boot version: 1.5.10.RELEASE

I'm facing the issue with auto configuration of ObjectMapper when @EnableWebMvc annotation is present on configuration class.

First of all I'm trying to specify Jackson customization via application.yml for including only non null/empty fields:

spring:
  jackson:
    default-property-inclusion: non_absent

@Primary ObjectMapper is being created via JacksonAutoConfiguration#jacksonObjectMapper.

Also RequestMappingHandlerAdapter is being created from WebMvcConfigurationSupport#requestMappingHandlerAdapter and during it's initialization new instance of MappingJackson2HttpMessageConverter is created which also creates new ObjectMapper instance effectively ignoring customizations given in application.yml file.

Basically after application is up and running MappingJackson2HttpMessageConverter is taken from MVC configuration step.

For now I did a hack in my class which extends MappingJackson2HttpMessageConverter to workaround the issue:

@Configuration
@EnableWebMvc
class MvcConfig extends WebMvcConfigurerAdapter {
    private final ObjectMapper objectMapper;

    MvcConfig(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }

   // some project specific code goes here
}

So it takes the @Primary object mapper via constructor injection and I'm recreating message converter programmatically.

See https://github.com/rand0m86/spring-boot-issue-webmvc for example and test (hack is not included in the project).

Comment From: bclozel

Unless I'm missing something, I'm closing this issue as "invalid" because this works as expected.

As stated in the reference documentation, adding @EnableWebMvc to your application means that you want to take complete control of the Spring MVC auto-configuration. Configuring the Jackson message converter with the configuration properties is part of this.

Removing @EnableWebMvc should solve this issue; if this is intended, then I believe we can't solve that as it would go against developers' expectations: Spring Boot should back off as soon as we express a configuration opinion.

Thanks!

Comment From: rand0m86

Indeed @bclozel I missed the part about the impact of usage this annotation. Thank you.

Comment From: dalegaspi

There are at least 10 million questions about this in StackOverflow and not one has been able to provide a clear explanation on the use of custom WebMvcConfigurer and why you actually don't always want to use it in conjuction with @EnableWebMvc.

In my case, I wrote my own CORS filter by extending WebMvcConfigurer with @EnableWebMvc and was very confused why enabling the custom filter messes up with the JSON serialization of my REST Controllers (which uses the default Spring Jackson objects). This thread lead me to the answer and explanation.