小编Pus*_*wal的帖子

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

我已将注释放在类/ pojo上并且还配置了映射器,但它仍然序列化

我正在使用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;

}
Run Code Online (Sandbox Code Playgroud)

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);  
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

{"id":1,"students":null,"staff":null}
Run Code Online (Sandbox Code Playgroud)

java spring json hibernate jackson

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

hibernate ×1

jackson ×1

java ×1

json ×1

spring ×1