For serialising a java object’s name field in json, if I have a java class:
Class Test {
private String name;
}
When I create the Test object with a name.
In scenario one, when I serialise the test object, I would like the name field to be represented as name.
{ "name": "Bob"}
In scenario two , when I serialise the test object, I would like the name field to be represented as firstName.
{ "firstName": "Bob"}
How can I achieve this with jackson.
Thanks
2
Answers
You have to use
@JsonProperty
annotation to set the desired custom field name that you want to show on serialization.You can user Views.
Views
DTO
Test