Why when a constructor is annotated with @JsonCreator, its arguments must be annotated with @JsonProperty?
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?
Solution 1:
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.
Solution 2:
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:
jackson-module-parameter-names
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
jackson-module-paranamer
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
Solution 3:
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
- The Java™ Tutorials - Obtaining Names of Method Parameters
Solution 4:
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;
}
Solution 5:
Because Java bytecode does not retain the names of method or constructor arguments.