Reputation: 2785
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;
}
Upvotes: 19
Views: 30157
Reputation: 4479
In addition to solutions from Answer by @MicaelGantman that leverage getters/setters, making the fields public
(even without getters/setters) also works (without any field or class-level Annotation). public final
also works (when shielding the fields more).
Upvotes: 0
Reputation: 7790
@JsonProperty is not a 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.
Upvotes: 7
Reputation: 143
@JsonRootName(value = "user")
public class User {
public int id;
public String name;
}
Upvotes: 1
Reputation: 956
Class level annotation
@JsonRootName("Response")
public class SuperResponse {
...
}
Result:
<Response>
...
</Response>
Upvotes: 12