gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:44 36 4
gpt4 key购买 nike

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

我正在使用 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相反。

关于java - @JsonInclude(Include.NON_NULL) 不工作/ jackson 序列化空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959267/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com