【发布时间】:2019-04-13 13:18:36
【问题描述】:
我正在尝试使用 spring boot 创建 json。
类:
public class Person {
private String name;
private PersonDetails details;
// getters and setters...
}
实现:
Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());
因此,Person 的实例为空,details 为空,这正是 Jackson 返回的内容:
"person": {
"name": "Apple",
"details": {}
}
我想要json 没有空括号{}:
"person": {
"name": "Apple"
}
这个问题对我没有帮助:
- How to tell Jackson to ignore empty object during deserialization?
- How to ignore "null" or empty properties in json, globally, using Spring configuration
更新 1:
我使用的是 Jackson 2.9.6
【问题讨论】:
-
用于忽略空的特定对象的自定义序列化器/反序列化器听起来是不错的解决方案。所以例如
@JsonInclude(Include.NON_NULL)在这种情况下不起作用,因为你没有空对象,你有没有填充属性的对象。为什么要忽略 JSON 中的空对象?
标签: java json spring-boot