Home > Java > javaTutorial > How Can I Prevent Jackson from Serializing Unfetched Lazy Attributes in Spring?

How Can I Prevent Jackson from Serializing Unfetched Lazy Attributes in Spring?

Barbara Streisand
Release: 2024-11-28 20:16:15
Original
154 people have browsed it

How Can I Prevent Jackson from Serializing Unfetched Lazy Attributes in Spring?

Addressing Lazy Object Serialization Issue with Jackson

Your aspiration to prevent Jackson from serializing unfetched lazy attributes has a straightforward resolution. Thanks to the valuable input from indybee, we've uncovered a solution that works with your current Spring, Hibernate, and Jackson versions.

Spring versions 3.1.2 and later introduce an enhanced MappingJackson2HttpMessageConverter that incorporates the functionality of the custom converter mentioned in the Spring 3.1 tutorial. To leverage this converter, extend your Spring configuration class from WebMvcConfigurerAdapter and override the configureMessageConverters method.

Within the configureMessageConverters method, add the MappingJackson2HttpMessageConverter with the Hibernate4Module registered. In Java config, this might resemble:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(jacksonMessageConverter());
    super.configureMessageConverters(converters);
}
Copy after login

For an XML configuration, create a custom HibernateAwareObjectMapper as suggested in the tutorial:

<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>
Copy after login

This solution seamlessly integrates the Hibernate4Module with Spring's default message converter, ensuring that unfetched lazy objects are not serialized by Jackson.

The above is the detailed content of How Can I Prevent Jackson from Serializing Unfetched Lazy Attributes in Spring?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template