@JsonInclude(Include.NON_NULL) 不工作/杰克逊序列化空值

@JsonInclude(Include.NON_NULL) not working/ jackson serializing null values

我已经在 class/pojo 上放置了注释并配置了映射器,但它仍然 serialize null values

我正在使用 Hibernate 4.3.7Final 和 Jackson 2.4.4。这些集合 延迟加载

Pojo:删除了 getter 和 setter

@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);  
    }
}

输出:

{"id":1,"students":null,"staff":null}

请尝试使用 JsonInclude.NON_EMPTY

您可能想要针对项目提交错误;可能是处理延迟加载的集合(确实需要特殊处理并覆盖默认集合)没有进行正确的包含检查。