使用 Jackson 忽略 JSON 对象上的新字段
我正在使用 Jackson JSON 库将一些 JSON 对象转换为 Android 应用程序上的 POJO 类。问题是,在发布应用程序时,JSON 对象可能会发生变化并添加新字段,但目前,即使添加一个简单的 String 字段(可以安全地忽略),它也会中断。
有没有办法告诉杰克逊忽略新添加的字段? (例如 POJO 对象上不存在)?全球忽视会很棒。
I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored.
Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Jackson 提供了可在类级别使用的注释 (JsonIgnoreProperties)。
将以下内容添加到类的顶部(而不添加到各个方法中):
根据您使用的 jackson 版本,您必须在当前版本中使用不同的导入:
在旧版本中,它已经:
Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).
Add the following to the top of your class (not to individual methods):
Depending on the jackson version you are using you would have to use a different import in the current version it is:
in older versions it has been:
除了已经提到的两种机制之外,还有一个全局功能可用于抑制由未知(未映射)属性引起的所有失败:
这是在没有注释的情况下默认使用的,并且可以方便地回退。
In addition to 2 mechanisms already mentioned, there is also global feature that can be used to suppress all failures caused by unknown (unmapped) properties:
This is the default used in absence of annotations, and can be convenient fallback.
最新完整答案
Jackson 2使用注释的
请参阅 JsonIgnoreProperties。
使用配置
比注释侵入性更小。
请参阅 FAIL_ON_UNKNOWN_PROPERTIES杰克逊在线文档。
Up to date and complete answer with Jackson 2
Using Annotation
See JsonIgnoreProperties on Jackson online documentation.
Using Configuration
Less intrusive than annotation.
See FAIL_ON_UNKNOWN_PROPERTIES on Jackson online documentation.
它可以通过两种方式实现:
标记 POJO 以忽略未知属性
配置用于序列化/反序列化 POJO/json 的 ObjectMapper,如下所示:
it can be achieved 2 ways:
Mark the POJO to ignore unknown properties
Configure ObjectMapper that serializes/De-serializes the POJO/json as below:
@JsonIgnoreProperties(ignoreUnknown = true)
对我来说效果很好。我有一个java应用程序,它在带有jdk 1.7的tomcat上运行。
@JsonIgnoreProperties(ignoreUnknown = true)
worked well for me. I have a java application which runs on tomcat with jdk 1.7.
如果使用基于 JSON 响应的 pojo 类。如果有可能 json 更改经常在 pojo 类级别声明:
并且在 objectMapper 中添加此内容(如果要转换):
这样代码就不会中断。
If using a pojo class based on JSON response. If chances are there that json changes frequently declare at pojo class level:
and at the objectMapper add this if you are converting:
So that code will not break.
从 Jackson 2.4 及更高版本开始,发生了一些变化。
现在您可以这样做:
................................................ ................................
有关更多信息,请参阅 10 分钟配置教程:
https://github.com /FasterXML/jackson-databindStarting with Jackson version 2.4 and above there have been some changes.
Here is how you do it now:
..........................................................................
For more information see the 10 minutes Configuration tutorial at:
https://github.com/FasterXML/jackson-databind确保将
@JsonIgnoreProperties(ignoreUnknown = true)
注释放置到要作为解析 JSON 响应的结果填充的父 POJO 类,而不是放置从 JSON 到 Java 的转换的类对象正在发生。Make sure that you place the
@JsonIgnoreProperties(ignoreUnknown = true)
annotation to the parent POJO class which you want to populate as a result of parsing the JSON response and not the class where the conversion from JSON to Java Object is taking place.如上所述,注释仅在父 POJO 类中指定时才起作用,而不是在发生从 JSON 到 Java 对象的转换的类中指定。
另一种不触及父类并造成中断的替代方案是仅为您所需的映射器方法实现您自己的映射器配置。
反序列化功能的包也已移动。
DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES 为
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES
As stated above the annotations only works if this is specified in the parent POJO class and not the class where the conversion from JSON to Java Object is taking place.
The other alternative without touching the parent class and causing disruptions is to implement your own mapper config only for the mapper methods you need for this.
Also the package of the Deserialization feature has been moved.
DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES
对于使用 Spring Boot 的用户,您可以使用
Jackson2ObjectMapperBuilder
配置 Jackson 的默认行为。例如:
然后你可以在任何需要的地方自动装配
ObjectMapper
(默认情况下,这个对象映射器也将用于http内容转换)。For whom are using Spring Boot, you can configure the default behaviour of Jackson by using the
Jackson2ObjectMapperBuilder
.For example :
Then you can autowire the
ObjectMapper
everywhere you need it (by default, this object mapper will also be used for http content conversion).我正在使用 jackson-xxx 2.8.5.Maven 依赖项,例如:
首先,如果您想全局忽略未知属性。您可以配置
ObjectMapper
。如下所示:
如果你想忽略某个类,你可以在你的类上添加注释
@JsonIgnoreProperties(ignoreUnknown = true)
,如下所示:I'm using jackson-xxx 2.8.5.Maven Dependency like:
First,If you want ignore unknown properties globally.you can config
ObjectMapper
.Like below:
If you want ignore some class,you can add annotation
@JsonIgnoreProperties(ignoreUnknown = true)
on your class like:您可以使用 @JsonIgnore 注释 POJO 中的特定属性。
You can annotate the specific property in your POJO with @JsonIgnore.