Hi guys,
Lets talk about it. We use this annotation when we try to map some json response to a pojo class variable. Like when are having a json variable in response whose name is something that we dont want to keep in our pojo class as it, like response is giving api call status in xxrStatus variable ,so it will it bit wrong approach to name variable same as such in our class too bcz its not following camelCase pattern.
For example :
OUR POJO LASS IS :
Class response {
String return;
}
And our json response is :
{
"Svcreturn" : "P"
}
Now as per java standard it will be little bit inappropriate to keeps variable name as Svcreturn in class. Because it is not following camel case pattern. But we can also not change it accordingly to camle case pattern because in response exact name will come and if we will try to map it to not matching uppercase and lowercase character wise then mapping wont happen too.
So @Jsonproperty give us a way to map json data value to different pojo class variable name without any issue
Note : must use Objectmapper class for mapping from jsonresonse string to pojo class.
For example : here our response class with @JsonProperty Annotation is as following:
Class response {
@JsonProperty("Svcreturn")
String return;
}
And our json response is :
{
"Svcreturn" : "P"
}
Now if will try to map the response data using mapper to pojo class by using below code then ,Svcreturn value will be mapped directly to return variable as it is having @Jsonproperty annotation above its with a name Svcreturn.
Objectmapper mapper = initialize it
mapper .map(jsonstringresponse , Response.class)
Same like @Jsonproperty there are other annotations that becomes quite useful for managing the code and resolving trivial conditions. Like @Jsonanygetter @JsonPropertOrder @JsonIgnoreProperty (Class Level) @JsonIgnore (Field Level) @JsonInclude @JsonFormat @JsonUnwrapped
We can also create a Custom Json annotation.
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({ "name", "id", "dateCreated" })
public @interface CustomAnnotation {}
Hope u found it helpful.