@JsonIgnore作用在属性和方法上,效果是一样的,都会屏蔽掉get、set方法
@JsonProperty作用在get、set方法上,可以保留get、set方法
例如:userName只接收,不返回:
public class UserDto {
private String userName;
private String password;
@JsonIgnore
public String getUserName() {
return userName;
}
@JsonProperty
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
通过以上两个注解实现password不接收,但返回结果,似乎有bug
https://github.com/FasterXML/jackson-databind/issues/1805
可以通过以下方法实现:
@JsonIgnoreProperties(value="password", allowGetters=true)
@JsonIgnoreProperties(value="password", allowGetters=true)
public class UserDto {
private String userName;
private String password;
@JsonIgnore
public String getUserName() {
return userName;
}
@JsonProperty
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}