springboot框架中在controller层使用@RestController注解,会将对象自动转换为json格式发送到前端。对象中会存在值为null的属性。如果不想在json数据中显示值为null的键值对,在配置文件中写入下面的代码
spring:
jackson:
default-property-inclusion: non-null
或者在实体类中通过注解@JsonInclude(Include.NON_NULL)挨个配置
public class Item {
private int itemId;
private String itemName; // 项目uuid
@JsonInclude(Include.NON_NULL)
private Area ar; // 区域
@JsonInclude(Include.NON_NULL)
private List<Area> area; // 区域集合
}
配置前
{
"status": true,
"message": "数据",
"data": [
{
"itemId": 1,
"itemName": "4a6494f1-d0f4-476a-9809-d3b09dcfc379",
"ar": null,
"area": [
{
"areaId": 1,
"areaName": "锌锅至出口",
"loca": null,
"location": [
{
"locationId": 1,
"locationName": "锌锅气刀",
"spot": {
"picCheck1": "pic1-1",
"picCheck2": "pic1-2",
"record": "气刀刀架锌渣、锌粉积聚",
"responsible": "生产清扫",
"examiner": "朱炜毛飞俊潘宏清",
"checkTime": "2020-12-25 14:41:59"
}
}
]
}
]
}
]
}
配置后
{
"status": true,
"message": "数据",
"data": [
{
"itemId": 1,
"itemName": "4a6494f1-d0f4-476a-9809-d3b09dcfc379",
"area": [
{
"areaId": 1,
"areaName": "锌锅至出口",
"location": [
{
"locationId": 1,
"locationName": "锌锅气刀",
"spot": {
"picCheck1": "pic1-1",
"picCheck2": "pic1-2",
"record": "气刀刀架锌渣、锌粉积聚",
"responsible": "生产清扫",
"examiner": "朱炜毛飞俊潘宏清",
"checkTime": "2020-12-25 14:41:59"
}
}
]
}
]
}
]
}