我可以在不显式定义getter的情况下将@JsonIgnore与来自lombok的@Getter注释一起使用吗,因为我必须在序列化对象时使用此JsonIgnore,但在反序列化时,必须忽略JsonIgnore注释,因此我的对象中的字段不能是无效?
@Getter
@Setter
public class User {
private userName;
@JsonIgnore
private password;
}
我知道,只要在 password 的 getter 上定义 JsonIgnore,我就可以防止我的密码被序列化,但为此,我必须明确定义我不想要的 getter。 请有任何想法,我们将不胜感激。
最佳答案
要将@JsonIgnore 放到生成的getter 方法中,可以使用onMethod = @__( @JsonIgnore )。这将生成带有特定注释的 getter。有关更多详细信息,请检查 http://projectlombok.org/features/GetterSetter.html
@Getter
@Setter
public class User {
private userName;
@Getter(onMethod = @__( @JsonIgnore ))
@Setter
private password;
}
关于java - @JsonIgnore 和@Getter 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466464/