When is the @JsonProperty property used and what is it used for?
Here's a good example. I use it to rename the variable because the JSON is coming from a .Net
environment where properties start with an upper-case letter.
public class Parameter {
@JsonProperty("Name")
public String name;
@JsonProperty("Value")
public String value;
}
This correctly parses to/from the JSON:
"Parameter":{
"Name":"Parameter-Name",
"Value":"Parameter-Value"
}
I think OldCurmudgeon and StaxMan are both correct but here is one sentence answer with simple example for you.
@JsonProperty(name), tells Jackson ObjectMapper to map the JSON property name to the annotated Java field's name.
//example of json that is submitted
"Car":{
"Type":"Ferrari",
}
//where it gets mapped
public static class Car {
@JsonProperty("Type")
public String type;
}
well for what its worth now... JsonProperty is ALSO used to specify getter and setter methods for the variable apart from usual serialization and deserialization. For example suppose you have a payload like this:
{
"check": true
}
and a Deserializer class:
public class Check {
@JsonProperty("check") // It is needed else Jackson will look got getCheck method and will fail
private Boolean check;
public Boolean isCheck() {
return check;
}
}
Then in this case JsonProperty annotation is neeeded. However if you also have a method in the class
public class Check {
//@JsonProperty("check") Not needed anymore
private Boolean check;
public Boolean getCheck() {
return check;
}
}
Have a look at this documentation too: http://fasterxml.github.io/jackson-annotations/javadoc/2.3.0/com/fasterxml/jackson/annotation/JsonProperty.html
Without annotations, inferred property name (to match from JSON) would be "set", and not -- as seems to be the intent -- "isSet". This is because as per Java Beans specification, methods of form "isXxx" and "setXxx" are taken to mean that there is logical property "xxx" to manage.