小编典典

为什么在使用@JsonCreator注释构造函数时,必须使用@JsonProperty注释其参数?

json

在Jackson中,当用注释构造函数时@JsonCreator,必须用注释其参数@JsonProperty。所以这个构造函数

public Point(double x, double y) {
    this.x = x;
    this.y = y;
}

变成这个:

@JsonCreator
public Point(@JsonProperty("x") double x, @JsonProperty("y") double y) {
    this.x = x;
    this.y = y;
}

我不明白为什么有必要。你能解释一下吗?


阅读 467

收藏
2020-07-27

共1个答案

小编典典

Jackson必须知道以什么顺序将字段从JSON对象传递给构造函数。使用反射无法在Java中访问参数名称-这就是为什么您必须在注释中重复此信息的原因。

2020-07-27