使用 JsonIgnoreProperties 特定 属性 反序列化属性仅存在于 JSON

Use of JsonIgnoreProperties specific property deserialize properties exists only in JSON

我偶然发现了一些将 JsonIgnoreProperties 添加到 属性 的代码,该 属性 不存在于 class,但存在于 JSON,例如:

@JsonIgnoreProperties({"ignoreprop"})
public class VO {
   public String prop;
}

JSON

{ "prop":"1", "ignoreprop":"9999"}

我想知道忽略属性在性能方面是否有任何优势,还是只是冗余代码?

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization).

编辑

是否有忽略特定 属性 的优势(与 @JsonIgnoreProperties(ignoreUnknown=true))?

从文档来看,使用这个的主要目的是无一例外地忽略JSON输入中的任何未知属性:最好不要在属性时弹出异常在 class 或 JSON 中都找不到,这可能有助于更快地序列化 docs

Example:

// to prevent specified fields from being serialized or deserialized

// (i.e. not include in JSON output; or being set even if they were included) @JsonIgnoreProperties({ "internalId", "secretKey" })

// To ignore any unknown properties in JSON input without exception: @JsonIgnoreProperties(ignoreUnknown=true)

Starting with 2.0, this annotation can be applied both to classes and to properties. If used for both, actual set will be union of all ignorals: that is, you can only add properties to ignore, not remove or override. So you can not remove properties to ignore using per-property annotation.

I wonder if ignoring properties has any advantage

是的,它在服务中被大量用于向前兼容。假设您有服务 A 和 B。目前 A 使用一些 JSON 对象向 B 发送请求。
现在您想在 JSON 中支持一个新的 属性。如果你有这个功能,你就可以让 A 在 B 知道如何处理之前开始发送新的 属性。 解耦这两个服务的开发过程。

ignoring specific property over all

这种情况确实有一些小的性能优势。首先,它不会尝试解析这个 属性,它可以是一个简单的字符串,也可以是一个复杂的 object/array。其次,它可以帮助您避免处理异常。认为以下所有都可以是有效调用而您只关心 prop:

{ "prop":"1", "ignoreprop":"9999"}

{ "prop":"1", "ignoreprop":{ "a": { "key": "value", "foo": false }}}

{ "prop":"1", "ignoreprop":[1,2,3,4,5,6..... 1000000]}