@JsonInclude (Include.NON_NULL) does not work / the serializer serializes null values

I posted the annotation above the / pojo class and also set up mapper but still serialize null

I am using Hibernate 4.3.7Final and Jackson 2.4.4. Collections are lazy loaded

Pojo: Removed getters and setters

@JsonInclude(Include.NON_NULL) @Entity @Table public class School { @Id @GeneratedValue private int id; @OneToMany(cascade=CascadeType.ALL,fetch= FetchType.LAZY) private List<Student> students; @OneToMany(cascade=CascadeType.ALL,fetch= FetchType.LAZY) private List<Employee> staff; } 

JSONMapper:

 @Component public class JSONMapper extends ObjectMapper { /** * */ private static final long serialVersionUID = -3131980955975958812L; //ref http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/ public JSONMapper() { Hibernate4Module hm = new Hibernate4Module(); registerModule(hm); configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); configure(SerializationFeature.INDENT_OUTPUT , false); configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); setSerializationInclusion(Include.NON_NULL); } } 

Exit:

 {"id":1,"students":null,"staff":null} 
+7
java json spring jackson hibernate
source share
2 answers

Try instead JsonInclude.NON_EMPTY .

+8
source share

You might want to indicate an error in the project; it may be that processing for lazy downloads (which require special handling and overriding by default) does not make valid inclusion checks.

+2
source share

All Articles