У меня есть класс адресов, который сопоставляется с другими классами, такими как город, штат, страна и т. Д. Когда я возвращаю объект адреса при весенней загрузке. В сериализации все внутренние классы. Чтобы избежать этого, я использую свойство @JsonIgnore
и создаю дополнительные методы получения и сеттеры для свойств, которые я хочу отобразить.
Мне нужно, чтобы state_name использовался для сериализации, а state_id для десериализации для объекта адреса.
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = Constants.HIBERNATE_UUID_GENERATED_VALUE_GENERATOR)
@GenericGenerator(name = Constants.HIBERNATE_UUID_GENERIC_GENERATOR_NAME, strategy = Constants.HIBERNATE_UUID_GENERIC_GENERATOR_STRATERGY)
@JsonProperty(value = AddressConstants.ADDRESS_ID_JSON_KEY, access = JsonProperty.Access.READ_ONLY)
@Column(name = AddressConstants.ADDRESS_ID_COLUMN_NAME, nullable = false, length = 64, unique = true)
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonProperty(value = AddressConstants.ADDRESS_LINE_1_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_LINE_1_COLUMN_NAME)
private String addressLine1;
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
@JsonProperty(value = AddressConstants.ADDRESS_LINE_2_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_LINE_2_COLUMN_NAME)
private String addressLine2;
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
@JsonProperty(value = AddressConstants.ADDRESS_POSTAL_CODE_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_POSTAL_CODE_COLUMN_NAME, nullable = false, length = 32)
private String postalCode;
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
@JsonProperty(value = AddressConstants.ADDRESS_EMAIL_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_EMAIL_COLUMN_NAME, nullable = false, length = 32, unique = true)
private String emailAddress;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@JsonProperty(value = AddressConstants.ADDRESS_MOBILE_NUMBER_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_MOBILE_NUMBER_COLUMN_NAME, nullable = false, length = 16, unique = true)
private long mobileNumber;
public long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(long mobileNumber) {
this.mobileNumber = mobileNumber;
}
@JsonProperty(value = AddressConstants.ADDRESS_PHONE_NUMBER_JSON_KEY)
@Column(name = AddressConstants.ADDRESS_PHONE_NUMBER_COLUMN_NAME, length = 16, unique = true)
private long phoneNumber;
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Column(name = AddressConstants.ADDRESS_ACTIVE_COLUMN_NAME, columnDefinition = Constants.HIBERNATE_BOOLEAN_DEFAULT_TRUE)
@JsonProperty(value = AddressConstants.ADDRESS_ACTIVE_JSON_KEY, access = JsonProperty.Access.WRITE_ONLY)
private Boolean active;
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
@OneToOne
@JoinColumn(name = AddressConstants.ADDRESS_CITY_COLUMN_NAME)
private City city;
@JsonIgnore
public City getCity() {
return city;
}
@JsonIgnore
public void setCity(City city) {
this.city = city;
}
@JsonGetter(AddressConstants.ADDRESS_CITY_NAME_JSON_KEY)
public String getCityName() {
return this.getCity().getName();
}
@JsonDeserialize
@JsonProperty(value = AddressConstants.ADDRESS_CITY_ID_JSON_KEY)
public void setCityId(String id) {
this.getCity().setId(id);
}
@OneToOne
@JoinColumn(name = AddressConstants.ADDRESS_STATE_COLUMN_NAME)
private State state;
@JsonIgnore
public State getState() {
return state;
}
@JsonIgnore
public void setState(State state) {
this.state = state;
}
@JsonGetter(AddressConstants.ADDRESS_STATE_NAME_JSON_KEY)
public String getStateName() {
return this.getState().getName();
}
@JsonProperty(AddressConstants.ADDRESS_STATE_ID_JSON_KEY)
public void setStateId(String id) {
this.getState().setId(id);
}
@OneToOne
@JoinColumn(name = AddressConstants.ADDRESS_COUNTRY_COLUMN_NAME)
private CountryEntity countryEntity;
@JsonIgnore
public CountryEntity getCountryEntity() {
return countryEntity;
}
@JsonIgnore
public void setCountryEntity(CountryEntity countryEntity) {
this.countryEntity = countryEntity;
}
@JsonGetter(AddressConstants.ADDRESS_COUNTRY_NAME_JSON_KEY)
public String getCountryName() {
return this.getCountryEntity().getName();
}
@JsonSetter(AddressConstants.ADDRESS_COUNTRY_ID_JSON_KEY)
public void setCountryId(String id) {
this.getCountryEntity().setId(id);
}
}