REST Assured 36 - @JsonInclude Annotation - Payload中忽略默认值字段

REST Assured 系列汇总 之 REST Assured 36 - @JsonInclude Annotation - Payload中忽略默认值字段

本文了解 Jackson library 中一个重要的注释 annotation @JsonInclude, 它有助于清除 默认,nullempty 等值,本文我们关注默认值。

有一个 Employee POJO 类:

public class Employee {
 
	// private variables or data members of pojo class
	private String firstName;
	private String lastName;
	private String gender;
	private int age;
	private double salary;
	private boolean married;
	
	// Getter and setter methods
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public boolean getMarried() {
		return married;
	}
	public void setMarried(boolean married) {
		this.married = married;
	} 	
}

一个 payload ,没有强制要求传递所有字段的值。例如:字段 gender 可能是可选的,没有必要在 payload 设置该值,需要传的 payload 如下:

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "age" : 29,
  "salary" : 3434343.0,
  "married" : true
}

不设置字段 gender 的值,序列化 Java Object 成 JSON Object 如下:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
 
public class AnnotationJsonIncludeExample {
 
	public static void main(String[] args) throws JsonProcessingException {
 
		// Just create an object of Pojo class
		Employee employee = new Employee();
		// Set value as you wish
		employee.setFirstName("Amod");
		employee.setLastName("Mahajan");
		employee.setAge(29);
		employee.setSalary(3434343);
		employee.setMarried(true);
		
		// Converting a Java class object to a JSON payload as string
		ObjectMapper objectMapper = new ObjectMapper();
		String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
		System.out.println(employeeJson);
	}
}

输出:

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : null,
  "age" : 29,
  "salary" : 3434343.0,
  "married" : true
}

观察一下上面的 payload,并不是我们需要的,因为不需要包括字段 gender。我们知道 类中的变量会有一个默认值,当我们没有显示地设置类中的变量值,在序列化时会用字段的默认值。因为字段 gender 是一个字符串类型,所有它默认的值是 null

再尝试一把,不设置 字段 age 的值:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
 
public class AnnotationJsonIncludeExample {
 
	public static void main(String[] args) throws JsonProcessingException {
 
		// Just create an object of Pojo class
		Employee employee = new Employee();
		// Set value as you wish
		employee.setFirstName("Amod");
		employee.setLastName("Mahajan");
		employee.setSalary(3434343);
		employee.setMarried(true);
		
		// Converting a Java class object to a JSON payload as string
		ObjectMapper objectMapper = new ObjectMapper();
		String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
		System.out.println(employeeJson);
	}
}

输出:

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : null,
  "age" : 0,
  "salary" : 3434343.0,
  "married" : true
}

可以看出在没有设置任何值时,整型类型字段 age 的默认值是 0

我们不想一个字段带着默认值包含在目标 payload 中,为此,我们需要用到 Jackson Java API 的 @JsonInclude annotation 在 POJO类中,使用范围可以是类级别和单个的属性级别。

import com.fasterxml.jackson.annotation.JsonInclude;
 
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Employee {
 
	// private variables or data members of pojo class
	private String firstName;
	private String lastName;
	private String gender;
	private int age;
	private double salary;
	private boolean married;
	
	// Getter and setter methods
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public boolean getMarried() {
		return married;
	}
	public void setMarried(boolean married) {
		this.married = married;
	} 	
}

我们在回到 AnnotationJsonIncludeExample 类中, 将会得到如下输出:

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "salary" : 3434343.0,
  "married" : true
}

@JsonInclude annotation 用于表明被注释的属性(某个字段,方法, 构造函数参数),或 被注释类的所有属性将要序列化。使用这个注释可以具体指定简单的排除规则来减少属性的输入。

Include 枚举有很多选项:

ALWAYS Value that indicates that property is to be always included, independent of value of the property.
CUSTOM Value that indicates that separate filter Object (specified by JsonInclude.valueFilter() for value itself, and/or
JsonInclude.contentFilter() for contents of structured types) is to be used for determining inclusion criteria.
NON_ABSENT Value that indicates that properties are included unless their value is: null “absent” value of a referential type (like Java 8 Optional, or {link java.utl.concurrent.atomic.AtomicReference}); that is, something that would not deference to a non-null value.
NON_DEFAULT Meaning of this setting depends on context: whether annotation is specified for POJO type (class), or not.
NON_EMPTY Value that indicates that only properties with null value, or what is considered empty, are not to be included.
NON_NULL Value that indicates that only properties with non-null values are to be included.
USE_DEFAULTS Pseudo-value used to indicate that the higher-level defaults make sense, to avoid overriding inclusion value.

上面的例子,我们将 @JsonInclude 加在 class 级别,将应用到 class 的所有成员。如果我们想针对某个字段,我们可以加在 属性级别,如下:

import com.fasterxml.jackson.annotation.JsonInclude;
 
public class Employee {
 
	// private variables or data members of pojo class
	@JsonInclude(JsonInclude.Include.NON_DEFAULT)
	private String firstName;
	@JsonInclude(JsonInclude.Include.NON_DEFAULT)
	private String lastName;
	@JsonInclude(JsonInclude.Include.NON_DEFAULT)
	private String gender;
	private int age;
	private double salary;
	private boolean married;
	
	// Getter and setter methods
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public boolean getMarried() {
		return married;
	}
	public void setMarried(boolean married) {
		this.married = married;
	} 	
}

如果 class 所有字段有相同的 annotation 那么最好应用 class 级别。但是如果用不同的 annotation 如 NON_NULL, NON_DEFAULT, NON_EMPTY 作用于不同的字段上,最好应用属性级别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值