当前位置:网站首页>When an entity is converted to JSON, the field with null value is lost
When an entity is converted to JSON, the field with null value is lost
2022-06-28 11:02:00 【Hello_ xzy_ Word】
Entity transfer JSON when , The value is null Missing fields for
- There is an entity class , The code is as follows :
@Data
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 2127997065197153097L;
private String name;
private String sex;
private Integer age;
private String phone;
}
- There is a Get Interface , The code is as follows :
@RequestMapping("student/")
@RestController
public class StudentController {
@GetMapping("get_student")
public StudentEntity getStudent() {
StudentEntity student = new StudentEntity();
student.setName(" Zhang San ");
student.setAge(23);
student.setSex(" male ");
// take ‘ Phone number ’ The assignment is null
student.setPhone(null);
return student;
}
}
- Call the interface , The data received are as follows :
{
"name": " Zhang San ",
"sex": " male ",
"age": 23,
"phone":null
}
- Add the following configuration to the configuration file :
spring.jackson.default-property-inclusion=non_null
- Restart project , Call the interface again , The data received are as follows :
{
"name": " Zhang San ",
"sex": " male ",
"age": 23
}
You can find , The value is null Of ’phone’ Not sent to front end .
- Remove section 4 Step to add the configuration , Yes StudentEntity Modify the entity class as follows :
@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;
}
- Restart project , Call the interface again , The data received are as follows :
{
"name": " Zhang San ",
"sex": " male ",
"age": 23
}
summary
stay Spring In the project , When the back end sends entity data to the front end , The rejection value is null There are two ways to use the fields of :
- Modify the configuration file , Add the following configuration :
spring.jackson.default-property-inclusion=non_null
- Modify entity class , Add the following comments to the required fields :
@JsonInclude(JsonInclude.Include.NON_NULL)
Empathy , Some key value pairs in the data received by the front end from the back end " take wings to itself ", The back end can check whether the system has added the above configuration or annotation .
边栏推荐
- Mongo database
- Metersphere uses JS to refresh the current page
- JSON module, hashlib, Base64
- 毕业季,给初入社会的你一些建议
- 将浏览器中的文件 url转换为File流
- 远程登录sshd服务
- JS foundation 2
- Internet of things application case of wireless module transparent transmission technology
- MytipartFile与File的相互转换
- Metersphere implements UI automation elements that are not clickable (partially occluded)
猜你喜欢

Summary of spatial temporal time series prediction modeling methods
![[monkey] Introduction to monkey test](/img/70/5a7152d0b6b77df7f9d6ad0e09e000.png)
[monkey] Introduction to monkey test

Katalon当中的output使用方法

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

Information hidden in the trend chart of Hong Kong London gold market

Oracle 日期格式化异常:无效数字

Katalon全局变量在TestObject引用

静态库的制作和使用

数据库系列:有什么办法对数据库的业务表进行无缝升级

How does ETF position affect spot gold price?
随机推荐
Wireshark数据抓包分析之FTP协议
Katalon当中的使用循环for、while和if...else、break、continue
李宏毅《机器学习》丨7. Conclusion(总结)
Set up your own website (11)
Katalon framework tests web (XX) custom keywords and upload pop-up operations
[leetcode daily question] [December 19, 2021] 997 Find the town judge
MySQL(三)
Oracle 日期格式化异常:无效数字
Summary of spatial temporal time series prediction modeling methods
Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
动态库(共享库)的制作和使用
Ruoyi integrated building block report (NICE)
关于Pytorch中双向LSTM的输出表示问题
metersphere使用js刷新当前页面
使用 Calendar 计算时间
AGCO AI frontier promotion (6.28)
JS foundation 4
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
[QT] connect syntax reference implementation
MySQL(二)