Jackson:忽略 Json 配置值 [英] Jackson: Ignore Json configuration value
本文介绍了Jackson:忽略 Json 配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 json 文件:
I have the following json file:
{
"segments": {
"externalId": 123,
"name": "Tomas Zulberti",
"shouldInform": true,
"id": 4
}
}
但是java模型如下:
But the java model is as follows:
public class Segment {
private String id;
private String name;
private boolean shouldInform;
// getter and setters here...
}
当 Jackson 解析它时会引发异常,因为externalId"字段没有 getter 或 setter.是否有可用于忽略 json 字段的装饰器?
When Jackson is parsing it raises an exception becuase there is no getter or setter for the field "externalId". It there a decorator that can be used to ignore a json field?
推荐答案
可以使用注解@JsonIgnoreProperties
;如果它只是您想跳过的一个值,例如:
You can use annotation @JsonIgnoreProperties
; if it's just one value you want to skip, something like:
@JsonIgnoreProperties({"externalId"})
或者忽略任何不能使用的东西:
or to ignore anything that can't be used:
@JsonIgnoreProperties(ignoreUnknown=true)
还有其他方法可以做到这一点,其余的请查看 FasterXML Jackson wiki.
There are other ways to do it too, for rest check out FasterXML Jackson wiki.
这篇关于Jackson:忽略 Json 配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文