Viratan
Viratan

Reputation: 521

How to omit a field in JSON returned value in Spring

I have a MongoDB which stores user information including the user's password. Users can call a GET request to retrieve all the user's information from the database. But I do not want the client to see the password, just every thing else.

Here, the client can get a user's personal info provided he has his username:

/* Get user by username */
    @RequestMapping(method=RequestMethod.GET, value="/getByUsername")
    public @ResponseBody User getUserByUsername(@RequestParam("username") String username) {
        return repo.findByUsername(username);
    }

the returned statement is in a JSON format like this:

{ id: "559e8d522de3b3e03b06457c" firstname: "bob" lastname: "alice" username: "username" password: "testpassword" address: "2020 america road" city: "The City" state: "maryland" zipCode: "99999" phoneNumber: "3421345" email: "[email protected]" }

This is what is being sent to the client and what the client sees. But I want to omit the password field from this JSON document.

How do I do that?

Upvotes: 1

Views: 113

Answers (1)

Sam Berry
Sam Berry

Reputation: 7844

Mark the getter for password with @JsonIgnore.

Upvotes: 1

Related Questions