The @JsonPropertyOrder เป็น หมายเหตุ ที่จะใช้ใน ระดับชั้นเรียน . ใช้เป็นคุณสมบัติรายการของฟิลด์ที่กำหนดลำดับที่ฟิลด์สามารถปรากฏในสตริงที่เป็นผลมาจากวัตถุ JSON serialization คุณสมบัติที่รวมอยู่ในการประกาศคำอธิบายประกอบสามารถถูกทำให้เป็นอนุกรมก่อน (ตามลำดับที่กำหนด) ตามด้วยคุณสมบัติใดๆ ที่ไม่รวมอยู่ในคำจำกัดความ
ไวยากรณ์
public @interface JsonPropertyOrder
ตัวอย่าง
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.*;
import java.io.*;
public class JsonPropertyOrderTest {
public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
Employee emp = new Employee();
emp.setFirstName("Adithya");
emp.setEmpId(25);
emp.setLastName("Jai");
emp.getTechnologies().add("Java");
emp.getTechnologies().add("Scala");
emp.getTechnologies().add("Python");
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp);
}
}
// Employee class
@JsonPropertyOrder({
"firstName",
"lastName",
"technologies",
"empId"
})
class Employee {
private int empId;
private String firstName;
private String lastName;
private List<String> technologies = new ArrayList<>();
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
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 List<String> getTechnologies() {
return technologies;
}
public void setTechnologies(List<String> technologies) {
this.technologies = technologies;
}
} ผลลัพธ์
{
"firstName" : "Adithya",
"lastName" : "Jai",
"technologies" : [ "Java", "Scala", "Python" ],
"empId" : 125
}