【问题标题】:How to hide some the fields of an object that are being mapped to JSON by Jackson and @JsonIgnore fails如何隐藏由 Jackson 和 @JsonIgnore 映射到 JSON 的对象的某些字段失败
【发布时间】:2016-10-05 04:50:09
【问题描述】:

我需要在我的响应对象中隐藏模型类中的一些字段。 I tried to follow this SO answer 但没有运气。

当一个字段有 getter 和 setter 时,@JsonIgnore 注释似乎不起作用。有关说明,请参见以下代码 sn-p。

@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;
  }

此外,这是对象映射的代码 sn-p

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;
}

任何有用的答案将不胜感激..谢谢

[更新] @JsonIgnore 可与 org.codehaus.jackson:jackson-core-asl:1.8.6 一起使用,但与 com.fasterxml.jackson.core:jackson-annotations:2.7.2 一起使用失败。知道为什么吗? ??

【问题讨论】:

  • jackson-core-asl 的等价物是 com.fasterxml.jackson.core:jackson-core,试试这个库吧。

标签: java json jackson jax-rs


【解决方案1】:

在 getter 方法中也添加 @JsonIgnore 注解。

或者尝试在班级级别添加@JsonIgnoreProperties(value={"name"}),如果这是您的选择

更新

如果您的类路径中有适当的 Jackson 库(组:'com.fasterxml.jackson.core',名称:'jackson-core'),您的字段上的@JsonIgnore 将正常工作;只要您拥有的 getter 方法是标准的 getter,您就不必使用 @JsonIgnore 注释 getter。

【讨论】:

  • 不,它不起作用。我也更新了 Q,我将 @JsonIgnore 应用于 getter,但它仍然不起作用
  • 您的 getName() 使用 @JsonProperty("name") 进行注释。您是否尝试删除它并改为添加 @JsonIgnore。这在 Spring 启动应用程序中为我服务
  • 伙计,我需要保持名称不变,但删除信息字段,我已经尝试过,但效果不佳
  • @Infamous 如果这是您的选择,请尝试在 Class 级别添加 @JsonIgnoreProperties(value={"name"})
  • 是的,只有当注释取自 org.codehaus.jackson:jackson-core 这有点旧并且 AFAIU 已停产时,它才有效。我希望 JsonIgnore 与 com.fasterxml.jackson.core:jackson-annotations 一起使用
【解决方案2】:

如果您想仅基于字段注释对对象进行序列化和反序列化,则应将 Jackson ObjectMapper 配置为忽略 getter 和 setter 方法:

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

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

也可以使用@JsonAutoDetect 注解在类级别进行配置。

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

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多