ใช้ Annotation @JsonAlias ระบุชื่อทางเลือกสำหรับ Property ระหว่าง Deserialization (JSON -> Object) ตอน Serialize (Object -> JSON) จะใช้ชื่อ Property จริง
Syntax
@JsonAlias({"alias1", "alias2", ...})
ตัวอย่างโค้ดครบถ้วน
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
// Employee Class
class Employee implements Serializable {
@JsonProperty("empId")
private Integer id;
@JsonProperty("empName")
@JsonAlias({"ename", "myename"}) // ชื่อทางเลือกสำหรับ Deserialize
private String name;
@JsonProperty("empTechnology")
private Technology tech;
// Constructors, Getters, Setters...
public Employee() {}
public Employee(Integer id, String name, Technology tech) {
this.id = id; this.name = name; this.tech = tech;
}
// Getters/Setters อาจลดรูปเพื่อความกระชับ
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Technology getTechnology() { return tech; }
public void setTechnology(Technology tech) { this.tech = tech; }
}
// Technology Class
class Technology implements Serializable {
@JsonProperty("skill")
private String skill;
@JsonProperty("subSkill")
@JsonAlias({"sSkill", "mySubSkill"})
private String subSkill;
public Technology() {}
public Technology(String skill, String subSkill) {
this.skill = skill; this.subSkill = subSkill;
}
public String getSkill() { return skill; }
public void setSkill(String skill) { this.skill = skill; }
public String getSubSkill() { return subSkill; }
public void setSubSkill(String subSkill) { this.subSkill = subSkill; }
}
// Main Test
public class JsonAliasExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT);
// Serialize: ใช้ชื่อ Property จริง (empName, subSkill)
Technology tech = new Technology("Java", "Oracle");
Employee emp = new Employee(110, "Raja", tech);
String json = mapper.writeValueAsString(emp);
System.out.println("=== Serialized JSON ===");
System.out.println(json);
// Deserialize: รับชื่อทางเลือกได้ (ename, sSkill)
String jsonInput = "{\"empId\":110,\"ename\":\"Raja\",\"empTechnology\":{\"skill\":\"Java\",\"sSkill\":\"Oracle\"}}";
Employee emp2 = mapper.readValue(jsonInput, Employee.class);
System.out.println("\n=== Deserialized Object ===");
System.out.println("Name: " + emp2.getName()); // Raja (จาก ename)
System.out.println("SubSkill: " + emp2.getTechnology().getSubSkill()); // Oracle (จาก sSkill)
}
}
ผลลัพธ์
=== Serialized JSON ===
{
"empId" : 110,
"empName" : "Raja",
"empTechnology" : {
"skill" : "Java",
"subSkill" : "Oracle"
}
}
=== Deserialized Object ===
Name: Raja
SubSkill: Oracle
สรุปการทำงาน
| ขั้นตอน | ชื่อ Property ที่ใช้ |
|---|---|
| Serialization (Object -> JSON) | ชื่อจริง / @JsonProperty value |
| Deserialization (JSON -> Object) | ชื่อจริง หรือ @JsonAlias values |
กรณีใช้งาน (Use Cases)
- รองรับ API Version ต่างๆ (เก่าใช้
enameใหม่ใช้empName) - Integrate กับ External System ที่命名 convention ต่างกัน (snake_case vs camelCase)
- Backward Compatibility เมื่อเปลี่ยนชื่อ Field