杰克逊:忽略Json配置值 [英] Jackson: Ignore Json configuration value

查看:113
本文介绍了杰克逊:忽略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...
}

当杰克逊解析时,它会引发异常,因为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.

这篇关于杰克逊:忽略Json配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆