Khi tuần tự hóa một đối tượng Java có tham chiếu đối tượng khác, tôi cần tuần tự hóa chỉ một thuộc tính của đối tượng lồng nhau (trường hợp thông thường của khóa ngoài, do đó, tuần tự hóa thuộc tính "id" của đối tượng tài liệu tham khảo). Nắm lấy mọi thứ khác.Jackson, tuần tự hóa một thuộc tính của tham chiếu
Ví dụ, tôi có hai lớp học mà tôi cần phải serialize để JSON & XML (loại bỏ chú thích JPA cho rõ ràng):
Mối quan hệ: User -> (one-to-many) AddressInformation; Ngoài ra: AddressInformation -> (one-to-one) tài
@XmlRootElement
public class User {
private String id;
private String firstName;
private String lastName;
private String email;
private AddressInformation defaultAddress;
private Set<AddressInformation> addressInformation;
public User() {
}
@JsonProperty(value = "id")
@XmlAttribute(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonProperty(value = "firstname")
@XmlAttribute(name = "firstname")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty(value = "lastname")
@XmlAttribute(name = "lastname")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@JsonProperty(value = "email")
@XmlAttribute(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@JsonIgnore
public Set<AddressInformation> getAddressInformation() {
return addressInformation;
}
public void setAddressInformation(Set<AddressInformation> addressInformation) {
this.addressInformation = addressInformation;
}
@JsonProperty(value = "defaultaddress")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public AddressInformation getDefaultAddress() {
return defaultAddress;
}
public void setDefaultAddress(AddressInformation defaultAddress) {
this.defaultAddress = defaultAddress;
}
}
AddressInformation:
@XmlRootElement
public class AddressInformation {
private String id;
private String address;
private String details;
private User user;
@JsonProperty(value = "id")
@XmlAttribute(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JsonProperty(value = "details")
@XmlAttribute(name = "details")
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
@JsonProperty(value = "address")
@XmlAttribute(name = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public AddressInformation() {
super();
}
}
enter code here
Khi serializing tài khoản ví dụ, tôi cần:
{
"id" : "idofuser01",
"email" : "[email protected]",
"status" : "OK",
"firstname" : "Filan",
"lastname" : "Ovni",
"defaultaddressid" : "idofaddress01",
}
enter code here
Khi serializing AddressInformation :
{
"id" : "idofaddress01",
"address" : "R.8. adn",
"details" : "blah blah",
"userid" : "idofuser01",
}
Tôi đã thử @JsonManageReference
& @JsonBackReference
không thành công. Như bạn thấy, tôi cũng đã thử @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Công trình này, nhưng tôi không hiểu nó đang làm gì. Bạn có thể xây dựng ? – Setheron
Có, bất cứ ai có thể giải thích lý do tại sao ở trên là làm việc? Cảm ơn. – HopeKing
Tôi thường đặt '@ JsonIdentityInfo' ở cấp lớp và' @ JsonIdentityReference' ở cấp độ phân bổ (một tham chiếu đến lớp khác). '@ JsonIdentityInfo' tránh đệ quy vô hạn khi tuần tự hóa (như User1-> Address1-> User1-> Address1 -> ...) viết trường" id "của thực thể được tham chiếu thay vì cố gắng tuần tự hóa POJO hoàn chỉnh. NHƯNG hành vi chính là serialize sự xuất hiện đầu tiên POJO hoàn toàn, và tham khảo thêm chỉ viết 'id'. '@ JsonIdentityReference' định cấu hình lại hành vi để luôn viết id được tham chiếu, không được tuần tự hóa toàn bộ POJO; như vậy: Address1 -> "John". –