@JsonPropertyOrder 주석 입니다. 클래스 수준에서 사용 . 객체 JSON 직렬화로 인해 문자열에 필드가 나타날 수 있는 순서를 정의하는 필드 목록을 속성으로 사용합니다. 주석 선언에 포함된 속성은 먼저(정의된 순서대로) 직렬화할 수 있으며, 정의에 포함되지 않은 모든 속성이 뒤따를 수 있습니다.
구문
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 }