REST Assured 系列汇总 之 REST Assured 29 - 创建POJO 作为一个JSON Object Payload
前面一节,我们了解了POJO(Plain Old Java Object),POJO类很广泛地用来为 API 创建JSON 和 XML payload,我们来了解一下POJO跟API Payload的联系。
一个简单的JSON例子: 下面是一个JSON结构,一对一的mapping关系。
{
"firstName": "Amod",
"lastName": "Mahajan",
"gender": "Male",
"age": 28,
"salary": 10000.56,
"married": false
}
让我们一步步地创建POJO
为POJO类识别字段或变量 观察上面的JSON,我们有字段 firstName, lastName , gender, age , salary 和 married。每个字段都有对应的值。现在关注一下值的类型,firstName 和 lastName 有 String 的值,age 有 integer 的值,salay有 double 的值, married有 boolean的值。让我们把字段名和数据类型对应起来。
firstName - String
lastName - Sring
age - int
gender - String
salary - double
married - boolean
我们现在在POJO类里来创建变量。因为存储的是一个员工的信息,所以我们将POJO类名为Employee,而且使得所有的变量为 private,这样没有人能直接操作这些变量。
public class Employee {
private String firstName;
private String lastName;
private String gender;
private int age;
private double salary;
private boolean married;
}
为 private 变量添加 getter 和 setter 方法 因为我们创建的所有变量都是 private 的,我们应该提供方法去获取或操作这些数据,所有我们需要为每个变量添加 getter 和 setter 方法。当然这不是强制的,取决于我们的需求。但是现在我们在一个类里添加 getter 和 setter 方法。一个 getter 方法是为了获取一个变量的值,一个 setter 方法是为了从外面能设置这个变量的值。
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;
}
你如果正担心,手动为大量的变量创建这些 getter 和 setter 方法会是非常痛苦的。大可不必有这种担心,每个IDE都有捷径添加 getter 和 setter 方法,goole一下就知道。
也不用在类里加任何构造函数,因为会有一个默认的构造函数。
完整的POJO类:
public class Employee {
private String firstName;
private String lastName;
private String gender;
private int age;
private double salary;
private boolean married;
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;
}
}
我们现在完成了为一个employee JSON创建一个POJO类,那么这个类有啥帮助呢。
用上面的POJO类,我们可以创建任意数量的自定义Employee对象,每个对象可以被转换成一个JSON Object,同样每个JSON Object 也被解析成 Employee POJO 对象。如果你有一个API需要动态的 Employee payload,我们可以很方便地创建不同数据的 employee payloads,而不用创建固定数据的JSON objects。简单来说,就是POJO方便你用简单的方式灵活创建和操作数据。
我们将从 POJO 创建一个JSON Object,反之亦然,利用Jackson API,我们称之为序列化和反序列化。
序列化 serialization - 将 Employee 类对象 转换成 JSON Object 呈现。
反序列化 deserialization - 将一个 JSON Object 转换成 Employee 类对象。
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EmployeeSerializationDeserialization {
@Test
public void createEmployeeJSONFromEmployeePOJOClass() throws JsonProcessingException
{
Employee employee = new Employee();
employee.setFirstName("Amod");
employee.setLastName("Mahajan");
employee.setAge(29);
employee.setGender("Male");
employee.setSalary(3434343);
employee.setMarried(false);
ObjectMapper objectMapper = new ObjectMapper();
String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
System.out.println(employeeJson);
}
@Test
public void getPojoFromEmployeeObject() throws JsonProcessingException
{
Employee employee = new Employee();
employee.setFirstName("Amod");
employee.setLastName("Mahajan");
employee.setAge(29);
employee.setGender("Male");
employee.setSalary(3434343);
employee.setMarried(false);
ObjectMapper objectMapper = new ObjectMapper();
String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
Employee employee2 = objectMapper.readValue(employeeJson, Employee.class);
System.out.println("First Name of employee : "+employee2.getFirstName());
System.out.println("Last Name of employee : "+employee2.getLastName());
System.out.println("Age of employee : "+employee2.getAge());
System.out.println("Gender of employee : "+employee2.getGender());
System.out.println("Salary of employee : "+employee2.getSalary());
System.out.println("Marital status of employee : "+employee2.getMarried());
}
}
输出:
{
"firstName" : "Amod",
"lastName" : "Mahajan",
"gender" : "Male",
"age" : 29,
"salary" : 3434343.0,
"married" : false
}
First Name of employee : Amod
Last Name of employee : Mahajan
Age of employee : 29
Gender of employee : Male
Salary of employee : 3434343.0
Marital status of employee : false
|