@JsonPropertyOrder 定义序列化时属性的顺序
@JsonPropertyOrder
可用于定义序列化时属性的顺序,注解中包含的属性会首先被序列化(按照定义的顺序),然后是未被包含的属性。
用于类级别
@JsonPropertyOrder({"name", "phoneNumber","email", "salary", "id" })
public class Employee {
private String id;
private String name;
private int salary;
private String phoneNumber;
private String email;
...
}
public class MainEmployeeSerialization {
public static void main(String[] args) throws IOException {
Employee e = new Employee();
e.setId("1");
e.setName("Jack");
e.setSalary(3000);
e.setPhoneNumber("111-111-111");
e.setEmail("jack@example.com");
System.out.println("-- before serialization --");
System.out.println(e);
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(e);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"name":"Jack","phoneNumber":"111-111-111","email":"jack@example.com","salary":3000,"id":"1"}
不使用 @JsonPropertyOrder 的输出
-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"id":"1","name":"Jack","salary":3000,"phoneNumber":"111-111-111","email":"jack@example.com"}
按字母顺序
@JsonPropertyOrder#alphabetic
可以按照字母顺序排序属性:
@JsonPropertyOrder(alphabetic = true)
public class Employee2 {
private String id;
private String name;
private int salary;
private String phoneNumber;
private String email;
...
}
public class MainEmployeeSerialization2 {
public static void main(String[] args) throws IOException {
Employee2 e = new Employee2();
e.setId("1");
e.setName("Jack");
e.setSalary(3000);
e.setPhoneNumber("111-111-111");
e.setEmail("jack@example.com");
System.out.println("-- before serialization --");
System.out.println(e);
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(e);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"email":"jack@example.com","id":"1","name":"Jack","phoneNumber":"111-111-111","salary":3000}
用于字段级别
@JsonPropertyOrder
可以用于字段,主要用于支持 Map 条目的字母顺序。如果此注释也同时在类级别上使用,则属性将覆盖类级别的。
@JsonPropertyOrder({"otherDetails", "name"})
public class Project {
private String name;
@JsonPropertyOrder(alphabetic = true)
private Map<String, String> otherDetails;
...
}
public class MainProjectSerialization {
public static void main(String[] args) throws JsonProcessingException {
Project p = new Project();
p.setName("Scheduler");
p.setOtherDetails(Map.of("start-month", "Jan 2019", "supervisor",
"Jack", "budget", "3000"));
System.out.println("-- before serialization --");
System.out.println(p);
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(p);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Project{name='Scheduler', otherDetails={supervisor=Jack, start-month=Jan 2019, budget=3000}}
-- after serialization --
{"otherDetails":{"budget":"3000","start-month":"Jan 2019","supervisor":"Jack"},"name":"Scheduler"}
原文链接
Jackson JSON - Using @JsonPropertyOrder annotation to define serialized properties ordering