Jackson, serializuj jeden atrybut odniesienia

Podczas serializacji obiektu Java, który ma inne odwołania do obiektów, muszę serializować tylko jeden atrybut zagnieżdżonego obiektu (zwykły przypadek klucza obcego, więc serializować atrybut „id” odwołania do obiektu). Ingore wszystko inne.

Na przykład mam dwie klasy, które muszę serializować do JSON i XML (usunięto adnotacje JPA dla jasności):

Relacja: Użytkownik -> (jeden do wielu) Informacja o adresie; Również: AddressInformation -> (jeden do jednego) Użytkownik

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

Informacje o adresie:

@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

Na przykład podczas serializowania użytkownika potrzebuję:

{
  "id" : "idofuser01",
  "email" : "[email protected]",
  "status" : "OK",
  "firstname" : "Filan",
  "lastname" : "Ovni",
  "defaultaddressid" : "idofaddress01",
}
enter code here

Podczas serializowania informacji o adresie:

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

próbowałem@JsonManageReference & @JsonBackReference bez powodzenia. Jak widać, próbowałem też@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

questionAnswers(2)

yourAnswerToTheQuestion