When is the @JsonProperty property used and what is it used for?

  


When is the @JsonProperty property used and what is it used for? :

1. Try This

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.

Query Bro

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"
}


2. Try This

I think OldCurmudgeon and StaxMan are both correct but here is one sentence answer with a 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;
}


Jackson Annotations | @JsonProperty | Example | Simple Programming




Previous Post Next Post