Jackson:忽略 Json 配置值
我有以下 json 文件:
{
"segments": {
"externalId": 123,
"name": "Tomas Zulberti",
"shouldInform": true,
"id": 4
}
}
但 java 模型如下:
public class Segment {
private String id;
private String name;
private boolean shouldInform;
// getter and setters here...
}
当 Jackson 解析时,它会引发异常,因为字段“externalId”没有 getter 或 setter。是否有一个装饰器可以用来忽略 json 字段?
I have the following json file:
{
"segments": {
"externalId": 123,
"name": "Tomas Zulberti",
"shouldInform": true,
"id": 4
}
}
But the java model is as follows:
public class Segment {
private String id;
private String name;
private boolean shouldInform;
// getter and setters here...
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用注释
@JsonIgnoreProperties
;如果您只是想跳过一个值,例如:或忽略任何无法使用的值:
还有其他方法可以做到这一点,其余请查看 FasterXML Jackson wiki。
You can use annotation
@JsonIgnoreProperties
; if it's just one value you want to skip, something like:or to ignore anything that can't be used:
There are other ways to do it too, for rest check out FasterXML Jackson wiki.
我们也可以使用mapper.enable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
相反 @JsonIgnoreProperties(ignoreUnknown=true)
但对于特定属性我们可以使用
Also we can use mapper.enable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
instead @JsonIgnoreProperties(ignoreUnknown=true)
but for particular property we can use