Java Jackson:忽略 Json 配置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4168095/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:15:02  来源:igfitidea点击:

Hymanson: Ignore Json configuration value

javajsonHymanson

提问by tzulberti

I have the following json file:

我有以下 json 文件:


{
  "segments": {        
            "externalId": 123, 
            "name": "Tomas Zulberti", 
            "shouldInform": true, 
            "id": 4
   }
}

But the java model is as follows:

但是java模型如下:


public class Segment {

    private String id;
    private String name;
    private boolean shouldInform;

    // getter and setters here...
}

When Hymanson 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?

当 Hymanson 解析它时会引发异常,因为“externalId”字段没有 getter 或 setter。是否有可用于忽略 json 字段的装饰器?

采纳答案by StaxMan

You can use annotation @JsonIgnoreProperties; if it's just one value you want to skip, something like:

您可以使用注释@JsonIgnoreProperties;如果它只是您想跳过的一个值,例如:

@JsonIgnoreProperties({"externalId"})

or to ignore anything that can't be used:

或者忽略任何不能使用的东西:

@JsonIgnoreProperties(ignoreUnknown=true)

There are other ways to do it too, for rest check out FasterXML Hymanson wiki.

还有其他方法可以做到这一点,其余的请查看FasterXML Hymanson wiki

回答by SamDJava

Also we can use mapper.enable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES); instead @JsonIgnoreProperties(ignoreUnknown=true)

此外,我们可以使用mapper.enable(DeserializationFeature。FAIL_ON_IGNORED_PROPERTIES); 相反@JsonIgnoreProperties(ignoreUnknown=true)

but for particular property we can use

但是对于特定的属性,我们可以使用

@JsonIgnoreProperties({"externalId"})
public class Segment {

    private String id;
    private String name;
    private boolean shouldInform;

    // getter and setters here...
}