开一篇博客,纪念我逝去的两个小时。
Spring对请求数据Json的校验,有两个方法。先说第一个坑@JsonProperty.requried()
@JsonProperty.required
官方说法,2.6版本以上,只有使用了Creator Properties才有效:
Note that as of 2.6, this property is only used for Creator Properties, to ensure existence of property value in JSON: for other properties (ones injected using a setter or mutable field), no validation is performed. Support for those cases may be added in future. State of this property is exposed via introspection, and its value is typically used by Schema generators, such as one for JSON Schema.
什么是Creator Properties呢?下面的id和name。且必须是类的构造函数。
@JsonCreator
public Dou(@JsonProperty(value = "id", required = true) Long id,
@JsonProperty(value = "name", required = true) String name) {
this.id = id;