Want to Add @JsonIgnore property on Password in response Json

主宰稳场 提交于 2019-12-10 02:54:01

问题


I want to add @JsonIgnore of Jackson On the Password property of User domain such that I must be able to send the Json With password and It saves my data in Database but in response I don't want to show the password.

How can I acheive this please help me.

I tried to use it at the Domain level of the User where the properties are defined but it Totally Ignore the property in the getter and setter methods.

I have tried this

private String password;
 @JsonIgnore
        public String getPassword() {
            return this.password;
        } 

回答1:


Try using both @JsonIgnore and @JsonProperty in your class like this:

private String password;

@JsonIgnore
public String getPassword() {
    return password;
}

@JsonProperty
public void setPassword(String password) {
    this.password = password;
}



回答2:


You can do it this way too if you don't want to manually create the getters/setters (eg. when using lombok's @Data)

@JsonProperty(access = Access.WRITE_ONLY)
private String password;



回答3:


I think you should use DTO Pattern to set / change password.




回答4:


In my case it helped to do it like that:

@JsonProperty
private String password;

@JsonIgnore
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}


来源:https://stackoverflow.com/questions/32219037/want-to-add-jsonignore-property-on-password-in-response-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!