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:
Extend our spring config class from WebMvcConfigurerAdapter and override the method configureMessageConverters.
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!
jackson - do not serialize lazy objects
You can do this with the Jackson @JsonInclude
annotation.
According to the latest version's javadoc (2.4 right now) you can specify with a simple annotation if to include or not the annotated property if the field value is null or empty.
By default, it's JsonInclude.Include.ALWAYS
, and this means that even if your lazily not-loaded values are null, Jackson does include the property.
Specifying to don't include empty or null values can significantly reduce the size of the JSON response, with all the benefits included..
If you want to change this behavior, you can add the annotation at class-level or single property/getterMethod level.
Try to add the following annotations to the fields you don't want to include if empty:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@OneToMany(fetch = FetchType.LAZY, mappedBy = ("movie"),cascade = CascadeType.ALL)
private List<Genre> genre;
Jackson doesn't serialize hibernate lazy fetched field
You mixed up @JsonBackReference
and @JsonManagedReference
, a field marked with @JsonBackReference
will not be serialized, it should be on PhysicianNote#physician
and @JsonManagedReference
should be on notes
.
Related Topics
Display Indeterminate Jprogressbar While Batch File Runs
Java Comparison with == of Two Strings Is False
How to Map an Entity Field Whose Name Is a Reserved Word in JPA
Static Method in a Generic Class
Java: Multiple Class Declarations in One File
Jar Bundler Using Osxadapter Causing Application to Lag or Terminate
How to Fix an Unsatisfiedlinkerror (Can't Find Dependent Libraries) in a Jni Project
Concatenating Null Strings in Java
Maven Does Not Find Junit Tests to Run
Eclipse Can't Find Xml Related Classes After Switching Build Path to Jdk 10
How to Maintain Jtable Cell Rendering After Cell Edit
Auto Errors Detection in Intellij Idea
How to Write a Utf-8 File with Java
Why Can't Strings Be Mutable in Java and .Net
Java Wait Cursor Display Problem
Jrbeancollectiondatasource: How to Show Data from the Java.Util.List from Javabean
Multiple Wildcards on a Generic Methods Makes Java Compiler (And Me!) Very Confused