Jackson和JsonIgnore隐藏秘密领域

时间:2016-10-29 23:00:52

标签: java jackson dropwizard morphia

我正在尝试使用dropwizard + morphia + jackson(默认为dropwizard)的组合,但我无法使@JsonIgnore@JsonIgnoreProperties工作。我已经尝试了@JsonIgnoreProperties关于我不希望向我的API的消费者公开的属性的类定义(密码和盐),我还尝试了@JsonIgnore字段声明本身作为以及吸气剂和二传剂的每一种排列......现在有点不知所措。

编辑:这是模型:

@Entity(value = "user", noClassnameStored = true)
@Indexes({
    @Index(fields = {
        @Field(value = "email", type = IndexType.ASC)},
        options = @IndexOptions(unique = true, sparse = true)
    )
})
public class User {
  @Id
  private ObjectId id = new ObjectId();
  @JsonProperty
  private String email;
  @JsonProperty
  private byte[] password;
  @JsonProperty
  private byte[] salt = SecurityUtils.getSalt();
  @Reference
  private Person person = new Person();

  public String getId() {
    return id.toHexString();
  }

  public void setId(ObjectId id) {
    this.id = id;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  @JsonIgnore
  public byte[] getPassword() {
    return password;
  }

  @JsonIgnore
  public void setPassword(String password) {
    this.password = SecurityUtils.hashPassword(password.toCharArray(), this.getSalt());
  }

  @JsonIgnore
  public byte[] getSalt() {
    return salt;
  }

  @JsonIgnore
  public void setSalt(byte[] salt) {
    this.salt = salt;
  }

  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }
}

除了上述内容之外,我还尝试使用@JsonIgnoreProperties({"password", "salt"} public class User...来定义课程,并且仅在getter,setter等上使用@JsonIgnore

我正在使用morphia v1.2.1来坚持。现在我有一个基本的DAO,它正在扩展morphia的BasicDAO,而且目前主​​要是代理。如果它有帮助,可以发布该代码的片段。

1 个答案:

答案 0 :(得分:2)

密码和salt都标记为@JsonProperty,优先于setter和getter上的ignore。我想如果删除JsonPropety注释(或用JsonIgnore替换它),那么你想要忽略的那些字段实际上会被忽略。

相关问题