简体   繁体   English

如何隐藏由Jackson和@JsonIgnore映射到JSON的对象的某些字段失败

[英]How to hide some the fields of an object that are being mapped to JSON by Jackson and @JsonIgnore fails

I need to hide some of the fields in the model class in my response object. 我需要在响应对象中隐藏模型类中的某些字段。 I tried to follow this SO answer but with no luck. 我试图遵循这个答案,但是没有运气。

when there are getter and setters for a field then the @JsonIgnore annotation doesn't seem to be working. 当一个字段有getter和setter方法时,@ JsonIgnore注释似乎不起作用。 see the following code snippet for clarifications. 有关说明,请参见以下代码段。

@ApiModel(description = "")
public class APIInfoDTO  {

  private String id = null;

  @JsonIgnore //this field will not be hidden when getters and setters are defined..
  private String name = null;


  private String status = null;

  @JsonIgnore // this "info" field is hidden since there are no getters                                 and setters for this field 
  private String info = "adncusdvshbdvsbvhdb";

  /**
   **/
  @ApiModelProperty(value = "")
  @JsonProperty("id")
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }


  /**
   **/
  @ApiModelProperty(value = "")
  @JsonProperty("name")
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }


  /**
   **/


@ApiModelProperty(value = "")
  @JsonIgnore
  public String getDescription() {
    return description;
  }
  @JsonProperty("description")
  public void setDescription(String description) {
    this.description = description;
  }

furthermore this is the code snippet for object mapping 此外,这是对象映射的代码片段

public static APIInfoDTO fromAPIToInfoDTO(API api) {
    APIInfoDTO apiInfoDTO = new APIInfoDTO();
    apiInfoDTO.setDescription(api.getDescription());
    apiInfoDTO.setContext(api.getContext());
    apiInfoDTO.setId(api.getUUID());
    APIIdentifier apiId = api.getId();
    apiInfoDTO.setName(apiId.getApiName());
    apiInfoDTO.setVersion(apiId.getVersion());
    apiInfoDTO.setProvider(apiId.getProviderName());
    apiInfoDTO.setStatus(api.getStatus().toString());
    String providerName = api.getId().getProviderName();
    apiInfoDTO.setProvider(APIUtil.replaceEmailDomainBack(providerName));
    return apiInfoDTO;
}

any helpful answer would be highly appreciated.. Thanks 任何有用的答案将不胜感激..谢谢

[UPDATE] The @JsonIgnore works with org.codehaus.jackson:jackson-core-asl:1.8.6 but fails with com.fasterxml.jackson.core:jackson-annotations:2.7.2.. Any idea why??? [更新] @JsonIgnore与org.codehaus.jackson:jackson-core-asl:1.8.6一起使用,但与com.fasterxml.jackson.core:jackson-annotations:2.7.2。一起失败。

Add @JsonIgnore Annotation to the getter method as well. 也将@JsonIgnore注释添加到getter方法。

Or Try adding @JsonIgnoreProperties(value={"name"}) at Class level, if this is an option for you 或尝试在类级别添加@JsonIgnoreProperties(value={"name"}) (如果您可以这样做)

UPDATE 更新

If you have Proper Jackson Library in your classpath (group: 'com.fasterxml.jackson.core', name: 'jackson-core'), @JsonIgnore on your field will work just fine; 如果您在类路径中有正确的Jackson库(组:“ com.fasterxml.jackson.core”,名称:“ jackson-core”),则@JsonIgnore在您的字段上将正常工作; as long as the getter method you have is a standard getter, you don't have to annotate getter with @JsonIgnore. 只要您拥有的getter方法是标准的getter,就不必使用@JsonIgnore注释getter。

If you want to serialize and deserialize your object based only on fields annotations, the Jackson ObjectMapper should be configured to ignore getters and setters method: 如果只想基于字段注释对对象进行序列化和反序列化,则应该将Jackson ObjectMapper配置为忽略getters和setters方法:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE));

or 要么

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

It can also be configured at Class level using the @JsonAutoDetect annotation. 也可以使用@JsonAutoDetect批注在类级别进行配置。

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class APIInfoDTO  {
    // ...
}

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

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