Spring-boot: ObjectMapper config

Created on 2 Apr 2018  路  9Comments  路  Source: spring-projects/spring-boot

I'm having trouble configuring ObjectMapper instances in my app using 2.0.0.RELEASE. I've tried all the variations of configuration (config class that exposes an ObjectMapper bean that I construct, extending Jackson2ObjectMapperBuilder, defining a Jackson2ObjectMapperBuilderCustomizer and setting properties in the application.yml). I have a spring-boot test that has the configured ObjectMapper injected on it and this instance is configured correctly however the ObjectMapper that is serializing/deserializing objects for my rest controller doesn't.

I'm not using @EnableWebMvc and have the following starter dependencies

spring-boot-starter-aop
spring-boot-starter-json
spring-boot-starter-validation
spring-boot-starter-web

Any help would be appreciated, I'm sure there is something that I am missing but I've spent the better part of 3 hours trying to get this to work to no avail.

Thanks in advance

invalid

Most helpful comment

Ok, found the issue. Just leaving this here in case there is anyone else with a similar issue. Problem is that I am not directly using @EnableWebMvc, but I am extending WebMvcConfigurationSupport in order to add a interceptors to some routes. Doh

Solution is to implement WebMvcConfigurer and override addInterceptors.

All 9 comments

Can you please share a project that shows the issue.

I have been in your position. Took me a while to get it working consistently. The attached configuration worked for me, I hope it does for you too.

SerializerConfiguration.zip

I think MappingJackson2HttpMessageConverter is the bit I'm missing but tbh my understanding was that if I exposed an ObjectMapper (anno'd Primary), or employed any of the other 2 mechanisms to customize the ObjectMapper I wouldn't be required to do this. I'll give that a try. Thanks.

As far as I can remember, Spring Boot requires MappingJackson2HttpMessageConverter configuration for serializing response entities in controllers while ObjectMapper or XmlMapper is for DI purposes.

from my OP - I'm not using @EnableWebMvc

@kedar-joshi Thanks for trying to help, but, to make some progress here, I think we need a project from @steelThread that recreates the issue.

@philwebb i stood up a demo project and things worked as expected. added you as a collab to the actual project, which you aren't going to be able to run unfortunately. i obviously don't expect you to debug my code so i'm going to close this. if you do have a few mins to peek at the JacksonConfig, JacksonConfigTest and the OptOutResource to see if i'm doing something wrong, but it seems pretty standard. the symptom here is while in the spring unit test the deserializer is registered with the OM from the context but coming in through the controller it isn't.

thanks

Ok, found the issue. Just leaving this here in case there is anyone else with a similar issue. Problem is that I am not directly using @EnableWebMvc, but I am extending WebMvcConfigurationSupport in order to add a interceptors to some routes. Doh

Solution is to implement WebMvcConfigurer and override addInterceptors.

Thanks for following up, @steelThread. You may be interested in https://github.com/spring-projects/spring-boot/issues/9115. If you're using 2.0 and DevTools, each time your application restarts, a diff of what has been auto-configured is logged. It's hopefully as useful way to explore the effect of extending WebMvcConfigurationSupport or adding @EnableWebMvc to your application.

Was this page helpful?
0 / 5 - 0 ratings