Reputation: 71
I have an object named AddOnsSRO.Only on serialization I want the names of fields of the object to be changed.
Tried using @JsonProperty on getter methods but it gives me a renamed field even on usages where serialization is not involved.
public class AddOnsSRO {
private String sideCar;
private String sideCarCoverage;
@JsonSerialize
@JsonProperty("abc")
public String getSideCar() {
return sideCar;
}
public void setSideCar(String sideCar) {
this.sideCar = sideCar;
}
@JsonSerialize
@JsonProperty("xyz")
public String getSideCarCoverage() {
return sideCarCoverage;
}
public void setSideCarCoverage(String sideCarCoverage) {
this.sideCarCoverage = sideCarCoverage;
}
}
Only on serialization the following fields : sideCar
and sideCarCoverage
must be renamed to abc
and xyz
respectively.
For any other use except serialization the field names should be sideCar
and sideCarCoverage
only.
Please help and suggest changes or annotations accordingly.
Upvotes: 2
Views: 1455
Reputation:
your code looks good...Please upgrade your jackson lib... if you are using old
Upvotes: -1
Reputation: 58772
For effecting only serializing use @JsonGetter instead of @JsonProperty
@JsonGetter("abc")
public String getSideCar() {
return sideCar;
}
Getter means that when serializing Object instance of class that has this method (possibly inherited from a super class), a call is made through the method, and return value will be serialized as value of the property.
You can add @JsonSetter
to setter method for deserialize:
@JsonSetter("sideCar")
public void setSideCar(String sideCar) {
this.sideCar = sideCar;
}
Upvotes: 2