![](/img/trans.png)
[英]Jackson serialize simple one attribute ValueObject like Enum - without nesting
[英]Jackson, serialize one attribute of a reference
序列化具有其他對象引用的Java對象時,我只需要序列化嵌套對象的一個屬性(通常是外鍵的情況,因此序列化對象引用的“id”屬性)。 其他一切。
例如,我有兩個類,我需要序列化為JSON和XML(為清楚起見,刪除了JPA注釋):
關系:用戶 - >(一對多)地址信息; 另外:AddressInformation - >(一對一)用戶
@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;
}
}
地址信息:
@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
例如,在序列化用戶時,我需要:
{
"id" : "idofuser01",
"email" : "some.email@gmail.com",
"status" : "OK",
"firstname" : "Filan",
"lastname" : "Ovni",
"defaultaddressid" : "idofaddress01",
}
enter code here
序列化AddressInformation時:
{
"id" : "idofaddress01",
"address" : "R.8. adn",
"details" : "blah blah",
"userid" : "idofuser01",
}
我試過@JsonManageReference
和@JsonBackReference
沒有成功。 如你所見,我也試過@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
剛剛找到了使用Jackson 2.1+的方法。
使用(這將僅選擇AddressInformation
的id
屬性)注釋對象引用:
@JsonProperty(value = "defaultaddressid")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
public AddressInformation getDefaultAddress() {
return defaultAddress;
}
序列化工作得很好。
您可以為此類實現自定義反序列化器,並在User
類中使用它。 示例實現:
class AddressInformationIdJsonSerializer extends JsonSerializer<AddressInformation> {
@Override
public void serialize(AddressInformation value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.getId());
}
}
並在User
類中配置:
@JsonProperty(value = "defaultaddress")
@JsonSerialize(using = AddressInformationIdJsonSerializer.class)
public AddressInformation getDefaultAddress() {
return defaultAddress;
}
###實現一個接口的所有類的通用解決方案###
您可以創建包含String getId()
方法的接口:
interface Identifiable {
String getId();
}
此接口的Serializer可能如下所示:
class IdentifiableJsonSerializer extends JsonSerializer<Identifiable> {
@Override
public void serialize(Identifiable value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.getId());
}
}
現在,您可以將此序列化程序用於所有可Identifiable
實現。 例如:
@JsonProperty(value = "defaultaddress")
@JsonSerialize(using = IdentifiableJsonSerializer.class)
public AddressInformation getDefaultAddress() {
return defaultAddress;
}
當然: AddressInformation
必須實現這個接口:
class AddressInformation implements Identifiable {
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.