Reputation: 10662
In Jackson, when you annotate a constructor with @JsonCreator
, you must annotate its arguments with @JsonProperty
. So this constructor
public Point(double x, double y) {
this.x = x;
this.y = y;
}
becomes this:
@JsonCreator
public Point(@JsonProperty("x") double x, @JsonProperty("y") double y) {
this.x = x;
this.y = y;
}
I don't understand why it's necessary. Can you please explain?
Upvotes: 157
Views: 166023
Reputation: 876
Just come across it and got an answer somewhere. you can use below annotation since 2.7.0
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Point {
final private double x;
final private double y;
@ConstructorProperties({"x", "y"})
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
Upvotes: -1
Reputation: 484
One can simply use java.bean.ConstructorProperties annotation - it's much less verbose and Jackson also accepts it. For example :
import java.beans.ConstructorProperties;
@ConstructorProperties({"answer","closed","language","interface","operation"})
public DialogueOutput(String answer, boolean closed, String language, String anInterface, String operation) {
this.answer = answer;
this.closed = closed;
this.language = language;
this.anInterface = anInterface;
this.operation = operation;
}
Upvotes: 24
Reputation: 1470
Parameter names are normally not accessible by the Java code at runtime (because it's drop by the compiler), so if you want that functionality you need to either use Java 8's built-in functionality or use a library such as ParaNamer in order to gain access to it.
So in order to not having to utilize annotations for the constructor arguments when using Jackson, you can make use of either of these 2 Jackson modules:
This module allows you to get annotation-free constructor arguments when using Java 8. In order to use it you first need to register the module:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule());
Then compile your code using the -parameters flag:
javac -parameters ...
Link: https://github.com/FasterXML/jackson-modules-java8/tree/master/parameter-names
This other one simply requires you to register the module or configure an annotation introspection (but not both as pointed out by the comments). It allows you to use annotation-free constructor arguments on versions of Java prior to 1.8.
ObjectMapper mapper = new ObjectMapper();
// either via module
mapper.registerModule(new ParanamerModule());
// or by directly assigning annotation introspector (but not both!)
mapper.setAnnotationIntrospector(new ParanamerOnJacksonAnnotationIntrospector());
Link: https://github.com/FasterXML/jackson-modules-base/tree/master/paranamer
Upvotes: 69
Reputation: 2125
As precised in the annotation documentation, the annotation indicates that the argument name is used as the property name without any modifications, but it can be specified to non-empty value to specify different name:
Upvotes: 3
Reputation: 22224
It is possible to avoid constructor annotations with jdk8 where optionally the compiler will introduce metadata with the names of the constructor parameters. Then with jackson-module-parameter-names module Jackson can use this constructor. You can see an example at post Jackson without annotations
Upvotes: 32
Reputation: 1376
Because Java bytecode does not retain the names of method or constructor arguments.
Upvotes: 7
Reputation: 20422
Jackson has to know in what order to pass fields from a JSON object to the constructor. It is not possible to access parameter names in Java using reflection - that's why you have to repeat this information in annotations.
Upvotes: 150