Reputation: 2873
I have a JPA transient property in an entity which has a calculated value based on multiple fields in the POJO. All these calculations are done in the GETTER of that property.
But, Jackson doesnt seem to be using the GETTER when creating the JSON for that POJO.
How do I configure Jackson to use getter for the property?
My POJO looks something like below
@Entity
public class ProductSummaryEntity implements Serializable {
@Basic
private String field1;
// GETTER and SETTER for Field1
@Basic
private String field2;
// GETTER and SETTER for Field2
@Transient
private String field3;
public String getField3(){
setField3(field1 + field2);
return this.field3;
}
public void setField3(String temp){
this.field3=temp;
}
}
Upvotes: 1
Views: 1486
Reputation: 4533
No, I don't think you can serialize transient field unless there is something latest is there.
Upvotes: 0
Reputation: 1293
This link to a blog by @sghill has been posted on SO before and shows you how to customize the serialization process: https://www.sghill.net/how-do-i-write-a-jackson-json-serializer-deserializer.html
Essentially, annotate your POJO with @JsonSerialize(using = CustomSerializer.class)
and then implement a class CustomSerializer
that's extending from JsonSerializer
. In your implementation you can build the JSON however you like and calculate values on the fly or call your getters.
Upvotes: 1