当前位置:网站首页>实体转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)
同理,当前端从后端接收到的数据中部分键值对"不翼而飞",后端可以检查是否系统是否添加了如上配置或注解.
边栏推荐
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- 树莓派无需显示屏的VNC Viewer方式的远程连接
- The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
- Datetime and logging module
- Dear leaders, ask me if MySQL does not support early_ Offset mode? Unsupported star
- 【实操】Appium Settings app is not running after 5000ms
- To enhance the function of jupyter notebook, here are four tips
- DataEase安装升级
- Realization of a springboard machine
- How to use K-line diagram for technical analysis
猜你喜欢
![[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)](/img/02/4dbd97c8b8df1c96b7c8e1460aeda2.png)
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)

第六天 脚本与动画系统

Mysql通用二进制安装方式
![[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)](/img/e8/21d8d81a3d7b544687d6adc06ad4b1.png)
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)

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

阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

ruoyi集成积木报表(nice)

Katalon当中的output使用方法

Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface

Spatial-Temporal时间序列预测建模方法汇总
随机推荐
[QT] connect syntax reference implementation
MySQL(二)
[unity] built in rendering pipeline to URP
JS基础8
How to use K-line diagram for technical analysis
Summary of characteristics of five wireless transmission protocols of Internet of things
Fastposter v2.8.4 release e-commerce poster generator
[unity][ecs] learning notes (III)
datetime与logging模块
[monkey] Introduction to monkey test
Yann LeCun新论文:构建自动智能体之路
Redis database
【Qt】connect 语法参考实现
DataEase安装升级
Mysql通用二进制安装方式
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)
Internet of things application case of wireless module transparent transmission technology
Transactions proof in appliedzkp zkevm (10)
Threads and thread pools
Ribbon core source code analysis