简体   繁体   English

想要隐藏 Jackson 映射到 JSON 的 object 的某些字段

[英]Want to hide some fields of an object that are being mapped to JSON by Jackson

I have a User class that I want to map to JSON using Jackson.我有一个用户 class 我想 map 到 JSON 使用 Z7930C951E609E461E82260085Z。

public class User {
    private String name;
    private int age;
    private int securityCode;

    // getters and setters
}

I map this to a JSON string using -我 map 这个到 JSON 字符串使用 -

User user = getUserFromDatabase();

ObjectMapper mapper = new ObjectMapper();   
String json =  mapper.writeValueAsString(user);

I don't want to map the securityCode variable.我不想 map securityCode变量。 Is there any way of configuring the mapper so that it ignores this field?有什么方法可以配置映射器以使其忽略此字段?

I know I can write custom data mappers or use the Streaming API but I would like to know if it possible to do it through configuration?我知道我可以编写自定义数据映射器或使用流式 API 但我想知道是否可以通过配置来做到这一点?

You have two options: 您有两种选择:

  1. Jackson works on setters-getters of fields. 杰克逊在场地的安排者身上工作。 So, you can just remove getter of field which you want to omit in JSON. 因此,您可以删除要在JSON中省略的字段的getter。 ( If you don't need getter at other place.) (如果你不需要在其他地方使用吸气剂。)

  2. Or, you can use the @JsonIgnore annotation of Jackson on getter method of that field and you see there in no such key-value pair in resulted JSON. 或者,你可以在该字段的getter方法中使用Jackson@JsonIgnore 注释,并且在结果JSON中看到没有这样的键值对。

     @JsonIgnore public int getSecurityCode(){ return securityCode; } 

you also can gather all properties on an annotation class 您还可以收集注释类的所有属性

@JsonIgnoreProperties( { "applications" })
public MyClass ...

String applications;

Adding this here because somebody else may search this again in future, like me. 在这里添加这个是因为其他人可能会在将来再次搜索这个,就像我一样。 This Answer is an extension to the Accepted Answer 本答案是对接受的答案的延伸

You have two options:

1. Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)

2. Or, you can use the `@JsonIgnore` [annotation of Jackson][1] on getter method of that field and you see there in no such key-value pair in resulted JSON. 

        @JsonIgnore
        public int getSecurityCode(){
           return securityCode;
        }

Actually, newer version of Jackson added READ_ONLY and WRITE_ONLY annotation arguments for JsonProperty . 实际上, 较新版本的Jackson为JsonProperty添加了READ_ONLY和WRITE_ONLY注释参数 So you could also do something like this. 所以你也可以这样做。

@JsonProperty(access = Access.WRITE_ONLY)
private String securityCode;

instead of 代替

@JsonIgnore
public int getSecurityCode(){
  return securityCode;
}

If you don't want to put annotations on your Pojos you can also use Genson . 如果您不想在Pojos上添加注释,您也可以使用Genson

Here is how you can exclude a field with it without any annotations (you can also use annotations if you want, but you have the choice). 以下是如何在没有任何注释的情况下使用它排除字段(如果需要,也可以使用注释,但您可以选择)。

Genson genson = new Genson.Builder().exclude("securityCode", User.class).create();
// and then
String json = genson.serialize(user);

if you are using GSON you have to mark the field/member declarations as @Expose and use the GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() 如果您使用的是GSON,则必须将字段/成员声明标记为@Expose并使用GsonBuilder()。excludeFieldsWithoutExposeAnnotation()。create()

Don't forget to mark your sub classes with @Expose otherwise the fields won't show. 不要忘记用@Expose标记子类,否则字段不会显示。

I had a similar case where I needed some property to be deserialized (JSON to Object) but not serialized (Object to JSON) 我有一个类似的情况,我需要一些属性反序列化(JSON到对象)但不序列化(对象到JSON)

First i went for @JsonIgnore - it did prevent serialization of unwanted property, but failed to de-serialize it too. 首先我去了@JsonIgnore - 它确实阻止了不需要的属性的序列化,但是也没能对它进行反序列化。 Trying value attribute didn't help either as it requires some condition. 尝试value属性也无济于事,因为它需要一些条件。

Finally, working @JsonProperty with access attribute worked like a charm. 最后,使用access属性工作的@JsonProperty就像一个魅力。

Field Level: 现场等级:

public class User {
    private String name;
    private int age;
    @JsonIgnore
    private int securityCode;

    // getters and setters
}

Class Level: 班级:

@JsonIgnoreProperties(value = { "securityCode" })
public class User {
    private String name;
    private int age;
    private int securityCode;
}

I suggest you use this.我建议你用这个。

   @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
   private int securityCode;

This allows you to set the value of securityCode(especially if you use lombok @Setter) and also prevent the field from showing up in the GET request.这允许您设置 securityCode 的值(尤其是如果您使用 lombok @Setter)并且还可以防止该字段出现在 GET 请求中。

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

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