一、@JsonInclude(JsonInclude.Include.NON_DEFAULT ) 属性为默认值不序列化
当后端返回的值为默认值时,比如string的默认值为null,int的默认值为0。我们可以过滤掉它们,不发送到前端。
1.1示例:
实体类:
package com.ts.pojo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
/*明星实体类*/
@Data
@AllArgsConstructor
@JsonInclude(value = JsonInclude.Include.NON_DEFAULT)
public class Star {
private String name;//姓名
private int age;//年龄
private String sex;//性别
private int height;//身高
//3个参数的构造方法
public Star(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
//2个参数的构造方法
public Star(String name, String sex) {
this.name = name;
this.sex = sex;
}
}
controller层:
package com.ts.controller;
import com.ts.pojo.Star;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/star")
public class StarController {
@GetMapping("/all")
public Star allItem(){
return new Star("赵丽颖",35,"女",167);
}
@GetMapping("/three")
public Star twoItem(){
return new Star("刘亦菲",36,"女");
}
@GetMapping("/two")
public Star threeItem(){
return new Star("杨幂","女");
}
}
1.2测试结果:
测试链接1:http://localhost:9090/star/all
{
"name": "赵丽颖",
"age": 35,
"sex": "女",
"height": 167
}
测试链接2:http://localhost:9090/star/two
{
"name": "杨幂",
"sex": "女"
}
http://localhost:9090/star/two
测试链接3:http://localhost:9090/star/three
{
"name": "刘亦菲",
"age": 36,
"sex": "女"
}
http://localhost:9090/star/three
二、@JsonInclude(JsonInclude.Include.NON_EMPTY ) 属性为空("") 或者为 NULL 都不序列化
当后端返回的值为""或null时。我们可以过滤掉它们,不发送到前端。
2.1示例:
实体类:
package com.ts.pojo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
/*狗的实体类*/
@Data
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Dog {
private String name;//姓名
private int age;//年龄
private String sex;//性别
private String hobby;//爱好
/*3个参数的构造方法*/
public Dog(String name, int age, String hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
}
controller层:
package com.ts.controller;
import com.ts.pojo.Dog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dog")
public class DogController {
@GetMapping("/notEmpty")
public Dog notEmpty() {
//hobby为空,而sex未赋值默认为null
return new Dog("哈士奇", 15, "");
}
}
2.2测试结果:
测试链接:http://localhost:9090/dog/notEmpty
{
"name": "哈士奇",
"age": 15
}
结果只显示出name和age这2个属性的值,因为hobby为"",sex为null,不显示
三、@JsonInclude(JsonInclude.Include.NON_NULL ) 属性为NULL 不序列化
当后端返回的值为null时。我们可以过滤掉它们,不发送到前端。
3.1示例:
实体类:
package com.ts.pojo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Student {
private int id;//学号
private String name;//姓名
private int age;//年龄
private String sex;//性别
private String address;//家庭住址
//3个参数的构造方法
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
controller层:
package com.ts.controller;
import com.ts.pojo.Dog;
import com.ts.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/student")
public class StudentController {
@GetMapping("/notNull")
public Student notNull() {
//address,sex未赋值默认为null
return new Student(90180989,"李小龙",32);
}
}
3.2测试结果:
测试链接:http://localhost:9090/student/notNull
{
"id": 90180989,
"name": "李小龙",
"age": 32
}
结果只显示出id,name,age这3个属性的值,因为address,sex未赋值默认为null,不显示