How to avoid lazy fetching in JSON serialization using Spring JPA + Spring Web MVC data?

I have a solution using Spring Data JPA and a REST controller in Spring Web MVC. The storage provider is Hibernate.

The persistence level is created using the Spring Repositories, and there is a service level between the REST controller and the repository:

Entity <--> Repository <--> Service <--> Controller

At the entity level, I have @OneToMany fields with FetchType = LAZY.

When the REST controller performs serialization, fetching is performed, but in some cases this is undesirable.

I already tried with @JSONInclude Jackson's annotation, and serialization is still happening.

Can someone help me with a proven solution?

+4
source share
4 answers

If you understand correctly, you want to serialize only when the lazy loaded collection is loaded, but you do not want serialization to call a selection.

If so, you should use jackson-datatype-hibernate and added as their docs already explain

public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
        registerModule(new Hibernate4Module());
    }
}

than register

 <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- Use the HibernateAware mapper instead of the default -->
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="path.to.your.HibernateAwareObjectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

The module has parameters Feature.FORCE_LAZY_LOADING, which indicates whether the object should be loaded and then serialized, which is set to false by default, which, I believe, is the behavior you need.

+6
source

@JsonIgnore, . @JsonView, , . , @JsonView , , .

+1

, Hibernate4Module ( Lazy) MappingJackson2HttpMessageConverter , Spring , HttpMessageConverters.

, Spring config WebMvcConfigurerAdapter configureMessageConverters MappingJackson2HttpMessageConverter Hibernate4Module.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //Here we add our custom-configured HttpMessageConverter
    converters.add(jacksonMessageConverter());
    super.configureMessageConverters(converters);
}

// Here we register the Hibernate4Module into an ObjectMapper, 
// then use this custom ObjectMapper
// to the MessageConverter
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;

}
0

A simple architectural solution should not use the Entity model as a data transfer object. Make a simple POJO as a data transfer object. In conversion logic, you can easily put a try and catch block for a LazyInitializationException. And thus, your POJO is always serialized, and you can use it on your controller.

0
source

All Articles