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