Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a class level annotation for @JsonProperty

Tags:

java

jackson

Is there a class level annotation for jackson's @JsonProperty ? Without knowing what field names are in the class, I can be able to annotate the class with a annotation and it will map it for me automatically?

Currently I have to annotate each field with a JsonProperty, is there something I can do at the class level that servers the same purpose?

public class myEntity() {

@JsonProperty("banner")
private String banner;

@JsonProperty("menu")
private String menu;

}
like image 691
PhoonOne Avatar asked Feb 08 '16 17:02

PhoonOne


People also ask

What does @JsonProperty annotation do?

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.

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.

What does @JsonProperty mean in Java?

Java Prime Pack @JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

What is Jackson annotations used for?

The Jackson annotation @JsonCreator is used to tell Jackson that the Java object has a constructor (a "creator") which can match the fields of a JSON object to the fields of the Java object.


2 Answers

@JsonProperty is not class level annotation, and you don't need to mark your class with any annotation. If you provide your class name as an argument to parser it will know how to map it according to your getter methods. It is as if every getter method has been marked with @JsonProperty without any argument

like image 108
Michael Gantman Avatar answered Oct 09 '22 22:10

Michael Gantman


Class level annotation

@JsonRootName("Response")
public class SuperResponse {
...
}

Result:

<Response>
...
</Response>
like image 39
Kyrylo Semenko Avatar answered Oct 10 '22 00:10

Kyrylo Semenko