Pra*_*ain 19 java json jackson lombok
我可以使用@JsonIgnore和lombok中的@Getter注释而不显式定义getter,因为我必须在序列化对象时使用此JsonIgnore,但在反序列化时,必须忽略JsonIgnore注释,因此我的对象中的字段不能为null.
@Getter
@Setter
public class User {
private userName;
@JsonIgnore
private password;
}
Run Code Online (Sandbox Code Playgroud)
我知道,只需在密码的getter上定义JsonIgnore,我就可以阻止我的密码被序列化,但为此我必须明确定义我不想要的getter事物.如有任何想法,任何帮助将不胜感激.
Seb*_*ian 38
要将@JsonIgnore放到生成的getter方法中,可以使用onMethod = @__(@JsonIgnore).这将生成具有特定注释的getter.有关更多详细信息,请访问 http://projectlombok.org/features/GetterSetter.html
@Getter
@Setter
public class User {
private userName;
@Getter(onMethod = @__( @JsonIgnore ))
@Setter
private password;
}
Run Code Online (Sandbox Code Playgroud)
小智 11
最近我在使用 jackson-annotation 2.9.0 和 lombok 1.18.2 时遇到了同样的问题
这对我有用:
@Getter
@Setter
public class User {
@JsonIgnore
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
Run Code Online (Sandbox Code Playgroud)
所以基本上添加注释@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)意味着该属性只能为反序列化而写入(使用 setter),而不会在序列化时读取(使用 getter)
| 归档时间: |
|
| 查看次数: |
12501 次 |
| 最近记录: |