1

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?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Just to clarify: you placed debugger breakpoint there and it is never invoked, right? – Andremoniy Sep 19 '17 at 08:22
  • Exactly, my `ObjectMapper` bean is completely neglected (which is not the case for all the other beans in the same `MyConfig` class. – membersound Sep 19 '17 at 08:23
  • Can you provide more context for your question? From what you've shown it's not clear that `MyConfig` should be used. Are you running your application as a jar, a war, in your IDE, something else? What does its main method look like if it has one? A [minimal, complete, verifiable, example](/help/mcve) would make things much clearer. – Andy Wilkinson Sep 19 '17 at 08:48

1 Answers1

2

Here's Spring's documentation for customised ObjectMapper, this is what it says:

If you want to replace the default ObjectMapper completely, either define a @Bean of that type and mark it as @Primary, or, if you prefer the builder-based approach, define a Jackson2ObjectMapperBuilder @Bean. Note that in either case this will disable all auto-configuration of the ObjectMapper.

If defining ObjectMapper bean does not help, could you try defining Jackson2ObjectMapperBuilder bean instead?

Also, could you try defining the beans into a class annotated with @Configuration?

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • It turned out I had to move the bean definition to it's own `@Configuration` class. I don't know why it is skipped if in main `SpringBootApplication` class? – membersound Sep 19 '17 at 09:13
  • maybe `@SpringBootApplication` does not extend `@Configuration` annotation/interface? Anyway, you can mark the answer as accepted if it has helped you. – Darshan Mehta Sep 19 '17 at 09:30