Java – Jackson, serializing a referenced attribute

When serializing Java objects with other object references, I only need to serialize one attribute of the nested object (usually in the case of foreign keys, so I serialize the "Id" attribute of the object reference) Everything else

For example, I have two classes that need to be serialized as JSON & XML (JPA comments are removed for clarity):

Relationship: user – > (one to many) addressinformation; In addition: addressinformation – > (one-to-one) user

@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;
    }
}

Address information:

@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

For example, when serializing users, I need to:

{
  "id" : "idofuser01","email" : "some.email@gmail.com","status" : "OK","firstname" : "Filan","lastname" : "Ovni","defaultaddressid" : "idofaddress01",}
enter code here

When serializing addressinformation:

{
  "id" : "idofaddress01","address" : "R.8. adn","details" : "blah blah","userid" : "idofuser01",}

I tried @ jsonmanagereference & @ jsonbackreference without success As you can see, I also tried @ jsonidentityinfo (generator = objectidgenerators. Propertygenerator. Class, property = "Id")

Solution

Just found a way to use Jackson 2.1

Annotate the object reference with (this selects only the ID attribute of addressinformation):

@JsonProperty(value = "defaultaddressid")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
@JsonIdentityReference(alwaysAsId = true) 
public AddressInformation getDefaultAddress() {
    return defaultAddress;
}

Serialization works well

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>