2

I have confiured annotation introspector (source)

public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
            jacksonObjectMapperBuilder.annotationIntrospector(
                    new JacksonAnnotationIntrospector() {
                        @Override
                        public JsonPOJOBuilder.Value findPOJOBuilderConfig(AnnotatedClass ac) {
                            if (ac.hasAnnotation(
                                    JsonPOJOBuilder.class)) {//If no annotation present use default as empty prefix
                                return super.findPOJOBuilderConfig(ac);
                            }
                            return new JsonPOJOBuilder.Value("build", "");
                        }
                    }
            );
        }
    };
}

And everything works until I added @EnableWebMvc to Applciation class. Now all dtos which have lombok @Value and @Builder annotations are fileld with nulls. It seems that my annotation introspector was replaced some where by spring. But where? Spring boot documentation said that it is enough to define Jackson2ObjectMapperBuilderCustomizer bean.

Any ideas how configure/fix setting annotation introspectors with @EnableWebMvc?

Cherry
  • 31,309
  • 66
  • 224
  • 364
  • 3
    Don't add `@EnableWebMvc`. Spring boot sets up spring mvc automatically without it. Similar problems discussed [here](https://stackoverflow.com/questions/53364800/spring-boot-2-not-serializing-localdatetime#comment93640725_53364800) and [here](https://stackoverflow.com/a/51065174/3591528). – teppic Nov 22 '18 at 19:29
  • Add this as en anwser. – Cherry Nov 23 '18 at 06:07

1 Answers1

0

Let's first understand why we need to use @EnableWebMvc. Spring traditionally supports two types of configurations:

XML based configuration

Annotation based configuration

So if we want spring to support annotation we add @EnableWebMvc. But if we are using Spring Boot, it will auto-configure the equivalent of @EnableWebMvc for you. Unless you want to switch off all of Boot's opinions about how Spring MVC should be configured, you should not use @EnableWebMvc in your application.

Now in your case by giving a Jackson2ObjectMapperBuilderCustomizer you want jackson to customize the behavior but @EnableWebMvc is switching off any opinion your object mapper is providing.