Why when a constructor is annotated with @JsonCreator, its arguments must be annotated with @JsonProperty?

Ori Popowski picture Ori Popowski · Feb 20, 2014 · Viewed 105.3k times · Source

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?

Answer

Lukasz Wiktor picture Lukasz Wiktor · Feb 25, 2014

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.