@EnableWebMvc "disables" Jackson2ObjectMapperBuilderCustomizer how to fix?
Asked Answered
S

1

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?

Sheepshearing answered 22/11, 2018 at 19:21 Comment(2)
Don't add @EnableWebMvc. Spring boot sets up spring mvc automatically without it. Similar problems discussed here and here.Idoux
Add this as en anwser.Sheepshearing
P
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.

Projection answered 17/1, 2023 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.