New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible incompatibility with Spring Boot #1140

Closed
Jaap-van-Hengstum opened this Issue Jan 13, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@Jaap-van-Hengstum

Jaap-van-Hengstum commented Jan 13, 2016

I've yet to test this with a clean version of Spring Boot but my guess is that this issue happens there also.

Spring Boot in HttpMessageConverters first adds the Spring Boot configured MappingJackson2HttpMessageConverter and then it adds the default MappingJackson2HttpMessageConverter from Spring MVC.

This causes the ObjectMapperConfigured event to fire twice, first for the configured converter (which is actually used) and then for the default converter. So when you f.e. set a custom property naming strategy then in ObjectMapperBeanPropertyNamingStrategythis is overwritten by the second event.

I've added the following code to my project to fix this:

@Configuration
public class MyWebAutoConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper objectMapper = null;
        for (HttpMessageConverter converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jacksonConverter =
                        ((MappingJackson2HttpMessageConverter) converter);

                if (objectMapper == null) {
                    objectMapper = jacksonConverter.getObjectMapper();
                } else {
                    jacksonConverter.setObjectMapper(objectMapper);
                }
            }
        }
    }
}
@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Jan 13, 2016

This is a known issue #1051, thank you for researching and providing a work around. I believe the issue of ObjectMapper selection needs to be fixed for sure.

@dilipkrish dilipkrish added this to the 2.4.0 milestone Jan 13, 2016

@Jaap-van-Hengstum

This comment has been minimized.

Jaap-van-Hengstum commented Jan 13, 2016

The issue with Spring Boot is that it registers two MappingJackson2HttpMessageConverter converters. My guess is that the second converter is never used because both converters act upon the same media types (application/json). If that is the case then if there is more than one jackson converter, only the first converter for the application/json media type should fire te event.

(and Spring Boot should remove the redundant converter, but that's a Spring Boot issue)

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Jan 13, 2016

💡 moment! I think reversing the order will just fix it for 90% of the cases right?

@Jaap-van-Hengstum

This comment has been minimized.

Jaap-van-Hengstum commented Jan 13, 2016

💡 moment! I think reversing the order will just fix it for 90% of the cases right?

That would probably work in most cases.

dilipkrish added a commit that referenced this issue Jan 14, 2016

Fixed the object mapper configurer use the correct instance
This is a naive fix to the problem. Simply reversing the order in which the message processors are
iterated over will fix 80% of the reported issues with not using the correct object mapper.

fixes #1051, #1140

@dilipkrish dilipkrish closed this in 76e2f7d Mar 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment