Avoid Jackson serialization on non fetched lazy objects Avoid Jackson serialization on non fetched lazy objects spring spring

Avoid Jackson serialization on non fetched lazy objects


I finally found the solution! thanks to indybee for giving me a clue.

The tutorial Spring 3.1, Hibernate 4 and Jackson-Module-Hibernate have a good solution for Spring 3.1 and earlier versions. But since version 3.1.2 Spring have his own MappingJackson2HttpMessageConverter with almost the same functionality as the one in the tutorial, so we don't need to create this custom HTTPMessageConverter.

With javaconfig we don't need to create a HibernateAwareObjectMapper too, we just need to add the Hibernate4Module to the default MappingJackson2HttpMessageConverter that Spring already have and add it to the HttpMessageConverters of the application, so we need to:

  1. Extend our spring config class from WebMvcConfigurerAdapter and override the method configureMessageConverters.

  2. On that method add the MappingJackson2HttpMessageConverter with the Hibernate4Module registered in a previus method.

Our config class should look like this:

@Configuration@EnableWebMvcpublic class MyConfigClass extends WebMvcConfigurerAdapter{    //More configuration....    /* Here we register the Hibernate4Module into an ObjectMapper, then set this custom-configured ObjectMapper     * to the MessageConverter and return it to be added to the HttpMessageConverters of our application*/    public MappingJackson2HttpMessageConverter jacksonMessageConverter(){        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();        ObjectMapper mapper = new ObjectMapper();        //Registering Hibernate4Module to support lazy objects        mapper.registerModule(new Hibernate4Module());        messageConverter.setObjectMapper(mapper);        return messageConverter;    }    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {        //Here we add our custom-configured HttpMessageConverter        converters.add(jacksonMessageConverter());        super.configureMessageConverters(converters);    }    //More configuration....}

If you have an xml configuration, you don't need to create your own MappingJackson2HttpMessageConverter either, but you do need to create the personalized mapper that appears in the tutorial (HibernateAwareObjectMapper), so your xml config should look like this:

<mvc:message-converters>    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">        <property name="objectMapper">            <bean class="com.pastelstudios.json.HibernateAwareObjectMapper" />        </property>    </bean></mvc:message-converters>

Hope this answer be understandable and helps someone find the solution for this problem, any questions feel free to ask!


As of Spring 4.2 and using Spring Boot and javaconfig, registering the Hibernate4Module is now as simple as adding this to your configuration:

@Beanpublic Module datatypeHibernateModule() {  return new Hibernate4Module();}

ref: https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring


This is similar to accepted solution by @rick.

If you don't want to touch existing message converters configuration you can just declare a Jackson2ObjectMapperBuilder bean like:

@Beanpublic Jackson2ObjectMapperBuilder configureObjectMapper() {    return new Jackson2ObjectMapperBuilder()        .modulesToInstall(Hibernate4Module.class);}

Do not forget to add the following dependency to your Gradle file (or Maven):

compile 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.4'

Useful if you have a application and you want to keep the ability to modify Jackson features from application.properties file.