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?
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.