java 反序列化 JSON 对象时如何忽略特定属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17922798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ignore a specific property when deserializing a JSON object?
提问by Oliver
I am working against a existing REST interface. One of the incoming JSON objects contains a property called size
which I would like to ignore when deserializing this JSON object?
我正在针对现有的 REST 接口工作。传入的 JSON 对象之一包含一个名为的属性size
,我想在反序列化此 JSON 对象时忽略该属性?
My standard behavior is to fail on unknown property, so I cannot configure the used object mapper to ignore unknown properties.
我的标准行为是在未知属性上失败,因此我无法将使用的对象映射器配置为忽略未知属性。
回答by nutlike
Add the annotation @JsonIgnoreProperties("size")
to your POJO. See the JavaDoc for @JsonIgnoreProperties
at fasterxml.github.iofor more information.
将注释添加@JsonIgnoreProperties("size")
到您的 POJO。见的JavaDoc@JsonIgnoreProperties
在fasterxml.github.io以获取更多信息。
回答by Radouxca
If the goal is to ignore property ONLY by deserialization, but still serialize it (read-only), it is possible with @JsonIgnoreProperties(value={ "size" }, allowGetters= true)
.
如果目标是仅通过反序列化忽略属性,但仍对其进行序列化(只读),则可以使用@JsonIgnoreProperties(value={ "size" }, allowGetters= true)
.
More info here: https://fasterxml.github.io/Hymanson-annotations/javadoc/2.6/com/fasterxml/Hymanson/annotation/JsonIgnoreProperties.html#allowGetters()
更多信息:https: //fasterxml.github.io/Hymanson-annotations/javadoc/2.6/com/fasterxml/Hymanson/annotation/JsonIgnoreProperties.html#allowGetters()