@Data
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 2127997065197153097L;
private String name;
private String sex;
private Integer age;
private String phone;
}
@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’并没有被发送到前端.
@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)
同理,当前端从后端接收到的数据中部分键值对"不翼而飞",后端可以检查是否系统是否添加了如上配置或注解.
原文:https://www.cnblogs.com/XiaoZhengYu/p/13472890.html