Search code examples
javaspringrestspring-dataspring-data-rest

Spring Data Rest: Different resource returned from received


In Spring Data Rest, is it possible to return a different resource to a request, and receive a different resource object when reading from the request body?

An example when adding a user:

{"username": "admin", "password": "123456"}

An example when querying a user:

{"username": "admin"}

As you can see above, when someone adds a user, they would have to send the password, but I would like to not send the password when sending it as response.


Solution

  • You should just be able to add @JsonIgnore to the relevant property. That would always hide the password when the user object is marshalled.

    public class user {
        private String username;
        private String password;
    
        @JsonIgnore
        private String getPassword() {
            return password;
        }
    
        @JsonProperty
        private void setPassword(String password) {
            this.password = password;
        }
    }