简体   繁体   English

Jackson ObjectMapper 忽略所有没有注释的属性

[英]Jackson ObjectMapper ignore all properties that has no annotation

My target is is to convert jsonObject to Class.我的目标是将 jsonObject 转换为 Class。 I want to add only fields that are anotated in Class.我只想添加在类中注释的字段。 Example: json object holds 50 fields.示例:json 对象包含 50 个字段。 Class has 4 fields.类有 4 个字段。 I want to map only exact 4 fields without adding 46 addition ignores in class.我只想映射精确的 4 个字段,而不在课堂上添加 46 个加法忽略。

JSON: JSON:

{
  "id": "1",
  "name": "John",
  "Address": "Some Address 7009",
}

Class:班级:

public static class User {
    Integer id;
    String name;

    public User (@JsonProperty("id")Integer id, @JsonProperty("name")String name {
            this.id= id;
            this.name= name;
    }
    ....
}

User class has no address field.用户类没有地址字段。 My target is to exclude it, because it has no annotation.我的目标是排除它,因为它没有注释。

Annotate your class with @JsonIgnoreProperties , as following:使用@JsonIgnoreProperties注释您的类,如下所示:

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    ...
}

When ignoreUnknown is true , all properties that are unrecognized (that is, there are no setters or creators that accept them) are ignored without warnings (although handlers for unknown properties, if any, will still be called) without exception.ignoreUnknowntrue ,所有无法识别的属性(即,没有接受它们的设置者或创建者)都将被忽略而不会发出警告(尽管未知属性的处理程序,如果有,仍将被调用),无一例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM