Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonProperty annotation on field as well as getter/setter

I have inherited a certain bit code that has the @JsonProperty annotation on getter/setters. The purpose is so that when the object is serialized using the Jackson library, the fields have that specific name.

Current code:

private String fileName;  @JsonProperty("FILENAME") public String getFileName() {     return fileName; }  @JsonProperty("FILENAME") public void setFileName(String fileName) {     this.fileName = fileName; } 

Now for another tool, I need to annotate the field with JsonProperty as well. So this will be my changed code:

@JsonProperty("FILENAME") private String fileName;  @JsonProperty("FILENAME") public String getFileName() {     return fileName; }  @JsonProperty("FILENAME") public void setFileName(String fileName) {     this.fileName = fileName; } 

Has anyone used this same annotation on both - the field as well as the getter/setters? I looked around on the net but didn't see anything.

I have compiled & run the code but I'm not sure if this would this cause any problems down the road. Any thoughts on this?

like image 428
OceanBlue Avatar asked Jul 17 '12 16:07

OceanBlue


People also ask

What is the use of @JsonProperty annotation?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

Is JsonProperty annotation necessary?

You definitely don't need all those @jsonProperty . Jackson mapper can be initialized to sereliazie/deserialize according to getters or private members, you of course need only the one you are using.

How do you use an getter setter annotation?

You can also put a @Getter and/or @Setter annotation on a class. In that case, it's as if you annotate all the non-static fields in that class with the annotation. You can always manually disable getter/setter generation for any field by using the special AccessLevel.

What does JsonCreator annotation do?

Annotation Type JsonCreator Marker annotation that can be used to define constructors and factory methods as one to use for instantiating new instances of the associated class.


1 Answers

My observations based on a few tests has been that whichever name differs from the property name is one which takes effect:

For eg. consider a slight modification of your case:

@JsonProperty("fileName") private String fileName;  @JsonProperty("fileName") public String getFileName() {     return fileName; }  @JsonProperty("fileName1") public void setFileName(String fileName) {     this.fileName = fileName; } 

Both fileName field, and method getFileName, have the correct property name of fileName and setFileName has a different one fileName1, in this case Jackson will look for a fileName1 attribute in json at the point of deserialization and will create a attribute called fileName1 at the point of serialization.

Now, coming to your case, where all the three @JsonProperty differ from the default propertyname of fileName, it would just pick one of them as the attribute(FILENAME), and had any on of the three differed, it would have thrown an exception:

java.lang.IllegalStateException: Conflicting property name definitions 
like image 118
Biju Kunjummen Avatar answered Oct 22 '22 07:10

Biju Kunjummen