I want to create my own jackson ObjectMapper bean, as follows:
@SpringBootApplication
@AutoConfigureBefore(JacksonAutoConfiguration.class) //even this does not help
public class MyConfig extends SpringBootServletInitializer {
@Bean
@Primary
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(true).build(); //much more configurations
}
}
Problem: the bean is never created, but instead the default JacksonAutoConfiguration is executed:
package org.springframework.boot.autoconfigure.jackson;
@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class JacksonAutoConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build(); //this is always executed!
}
}
So somehow the ObjectMapper bean is not present yet when JacksonAutoConfiguration is evaluated. But why?
By debugging with breakpoints I can also see that my bean is never invoked!
BUT what I noticed: even though I have the @AutoConfigureBefore, the jackson autoconfig is still run before any of the beans in MyConfig. Strange?