当前位置:网站首页>实体转JSON时,值为null的字段的丢失问题
实体转JSON时,值为null的字段的丢失问题
2022-06-28 10:51:00 【Hello_xzy_Word】
实体转JSON时,值为null的字段丢失问题
- 有一实体类,其代码如下:
@Data
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 2127997065197153097L;
private String name;
private String sex;
private Integer age;
private String phone;
}
- 有一Get接口,其代码如下:
@RequestMapping("student/")
@RestController
public class StudentController {
@GetMapping("get_student")
public StudentEntity getStudent() {
StudentEntity student = new StudentEntity();
student.setName("张三");
student.setAge(23);
student.setSex("男");
// 将‘电话号码’赋值为null
student.setPhone(null);
return student;
}
}
- 调用该接口,接收到的数据如下:
{
"name": "张三",
"sex": "男",
"age": 23,
"phone":null
}
- 在配置文件添加以下配置:
spring.jackson.default-property-inclusion=non_null
- 重启项目,再次调用接口,接收到的数据如下:
{
"name": "张三",
"sex": "男",
"age": 23
}
可以发现,值为null的’phone’并没有被发送到前端.
- 移出第4步操作添加的配置,对StudentEntity实体类进行如下修改:
@Data
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 2127997065197153097L;
private String name;
private String sex;
private Integer age;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String phone;
}
- 重启项目,再次调用接口,接收到的数据如下:
{
"name": "张三",
"sex": "男",
"age": 23
}
总结
在Spring项目中,后端向前端发送实体数据时,剔除值为null的字段的方式可以有两种:
- 修改配置文件,添加如下配置:
spring.jackson.default-property-inclusion=non_null
- 修改实体类,在需要的字段上添加如下注解:
@JsonInclude(JsonInclude.Include.NON_NULL)
同理,当前端从后端接收到的数据中部分键值对"不翼而飞",后端可以检查是否系统是否添加了如上配置或注解.
边栏推荐
- Markdown -- basic usage syntax
- Understanding of FTP protocol
- JS基础8
- 物联网5种无线传输协议特点大汇总
- Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
- Ribbon core source code analysis
- 一种跳板机的实现思路
- MySQL common commands for viewing database performance
- Katalon global variable is referenced in testobject
- [leetcode daily question] [December 19, 2021] 997 Find the town judge
猜你喜欢

Realize an air conditioner with compose to bring you cool in summer

Interface automation framework scaffolding - Implementation of parametric tools

无线通信模块定点传输-点对多点的具体传输应用

如何利用k线图做技术分析
![[monkey] Introduction to monkey test](/img/70/5a7152d0b6b77df7f9d6ad0e09e000.png)
[monkey] Introduction to monkey test

How to use dataant to monitor Apache apisex

MySQL general binary installation method
![[unity][ecs] learning notes (II)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[unity][ecs] learning notes (II)

树莓派无需显示屏的VNC Viewer方式的远程连接

MySQL cannot be opened. Flash back
随机推荐
The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
MytipartFile与File的相互转换
Move command
JS基础8
Markdown -- basic usage syntax
Dataease installation upgrade
Blackmail virus of industrial control security
JS基础6
工控安全之勒索病毒篇
Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
Pop up and push in sequence of stack < difficulty coefficient >
[unity][ecs] learning notes (II)
[unity] built in rendering pipeline to URP
【monkey】monkey测试入门
如何利用k线图做技术分析
Ribbon core source code analysis
Summary of spatial temporal time series prediction modeling methods
How does ETF position affect spot gold price?
Discard Tkinter! Simple configuration to quickly generate cool GUI!
metersphere使用js刷新当前页面