I was recently going through a scenario where I wanted the different JSON property names during serialization and deserialization in Java spring boot.
Let’s say you have a microservice that returns a property foo
, now in your DTO you want to read this property as foo
, but while returning this DTO, you want to rename this property to bar
.
You can do so by annotating the getter and setter values of the property with different JsonProperty.
private String id; @JsonProperty("foo") private String getId(){ return id; } @JsonProperty("bar") private void setId(String id){ this.id = id; }
This will read the property foo
while deserialization or converting the JSON to POJO class and will return property bar
while serialization or converting the POJO to JSON.