Avoid Jackson serialization on non fetched lazy objects

前端 未结 14 1883
终归单人心
终归单人心 2020-11-22 13:02

I have a simple controller that return a User object, this user have a attribute coordinates that have the hibernate property FetchType.LAZY.

When I try to get this

相关标签:
14条回答
  • 2020-11-22 13:39

    I tried this, and worked:

    // custom configuration for lazy loading

    public static class HibernateLazyInitializerSerializer extends JsonSerializer<JavassistLazyInitializer> {
    
        @Override
        public void serialize(JavassistLazyInitializer initializer, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
            jsonGenerator.writeNull();
        }
    }
    

    and configure mapper:

        mapper = new JacksonMapper();
        SimpleModule simpleModule = new SimpleModule(
                "SimpleModule", new Version(1,0,0,null)
        );
        simpleModule.addSerializer(
                JavassistLazyInitializer.class,
                new HibernateLazyInitializerSerializer()
        );
        mapper.registerModule(simpleModule);
    
    0 讨论(0)
  • 2020-11-22 13:42

    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
    @EnableWebMvc
    public 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!

    0 讨论(0)
提交回复
热议问题