Skip to content
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

MappingJackson2HttpMessageConverter and _halObjectMapper not working with spring-hateos 2.0.1 and spring boot 3 #1961

Open
anishsharma opened this issue Apr 14, 2023 · 1 comment
Assignees
Labels
in: core Core parts of the project in: mediatypes Media type related functionality process: waiting for feedback

Comments

@anishsharma
Copy link

anishsharma commented Apr 14, 2023

We have a legacy app which we are updating to spring boot 3. We used to use spring-hateos 0.24 RELEASE and below code to register MappingJackson2HttpMessageConverter.

public static class RestServicesConfiguration implements WebMvcConfigurer  {

       @Override
        public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {

            converters.add(jsonConverter());
            this.configureMessageConverters(converters);
        }

       @Bean
        public MappingJackson2HttpMessageConverter jsonConverter() {
            final MappingJackson2HttpMessageConverter converter = new HalMappingJackson2HttpMessageConverter();
            return converter;
        }

private class HalMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        
            private HalMappingJackson2HttpMessageConverter() {
                setSupportedMediaTypes(Arrays.asList(
                        CURRENT_REST_VERSION_MEDIA_TYPE,
                        PREVIOUS_REST_VERSION_MEDIA_TYPE,
                        V2_REST_VERSION_MEDIA_TYPE
                ));
                final ObjectMapper halObjectMapper = (ObjectMapper) beanFactory.getBean("_halObjectMapper");
               
                final SimpleModule module = new SimpleModule("MySerializers");
                module.addSerializer(Date.class, new DateSerializer());
                halObjectMapper.registerModule(module);

                setObjectMapper(halObjectMapper);
            }
        }

As we have updated to spring-hateos 2.0.1 via spring-boot-starter-data-rest 3.0.2. Upon running executing the WAR file in Tomcat 10 we are getting below exception.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonConverter' defined in com.config.AppInitializer$RestServicesConfiguration: Failed to instantiate [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]: Factory method 'jsonConverter' threw exception with message: No bean named '_halObjectMapper' available

@odrotbohm
Copy link
Member

The customization model has changed significantly in 1.0 already. Jumping from 0.24 to 2.0 is quite a leap. That said, you should be able to simply declare a bean of type HalConfiguration, call ….withObjectMapperCustomizer(…) on it and apply your customizations there. Additional media types can be registered via ….withMediaType(…).

@odrotbohm odrotbohm self-assigned this May 3, 2023
@odrotbohm odrotbohm added process: waiting for feedback in: core Core parts of the project in: mediatypes Media type related functionality labels May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Core parts of the project in: mediatypes Media type related functionality process: waiting for feedback
Projects
None yet
Development

No branches or pull requests

2 participants