Skip to content
Advertisement

MappingJackson2HttpMessageConverter configuration is not being recognized in spring boot 2

I am working with spring for a while already and in my spring boot 1.5x’s projects always dealing with the LazyInitializationException with the next configuration (more downstairs) but now I am creating a new project with spring boot 2 and the same configuration is not being recognized further than WebMvcConfigurer now replace to the deprecated WebMvcConfigurerAdapter.

My configuration:

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(jacksonMessageConverter());
            WebMvcConfigurer.super.configureMessageConverters(converters);
        }
        public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
            MappingJackson2HttpMessageConverter messageConverter =
                    new MappingJackson2HttpMessageConverter();
    
            List<MediaType> supportedMediaTypes=new ArrayList<>();
            supportedMediaTypes.add(MediaType.APPLICATION_JSON);
            supportedMediaTypes.add(MediaType.TEXT_PLAIN);
            messageConverter.setSupportedMediaTypes(supportedMediaTypes);
    
            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new Hibernate5Module());
            messageConverter.setObjectMapper(mapper);
    
            messageConverter.setPrettyPrint(true);
    
            return messageConverter;
        }
}

But after that I get an error yet:

[WARN ] 2018-08-18 20:07:08.694 DESKTOP-ABFFHEJ --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy [com.ideatik.domain.TipoUsuario#3] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.ideatik.domain.TipoUsuario#3] - no Session (through reference chain: com.ideatik.domain.Usuario["tipoUsuario"]->com.ideatik.domain.TipoUsuario_$$_jvst9b7_2["nombre"])
[WARN ] 2018-08-18 20:07:08.695 DESKTOP-ABFFHEJ --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy [com.ideatik.domain.TipoUsuario#3] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.ideatik.domain.TipoUsuario#3] - no Session (through reference chain: com.ideatik.domain.Usuario["tipoUsuario"]->com.ideatik.domain.TipoUsuario_$$_jvst9b7_2["nombre"])

Advertisement

Answer

try to add @Bean in the method

@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
   .....
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement