springboot对象为null的属性在json中不显示的解决
作者:普通还不自信的程序员
这篇文章主要介绍了springboot对象为null的属性在json中不显示的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
springboot对象为null的属性在json中不显示
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" } } ] } ] } ] }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。