In advance, I'm not speaking of Content Negotiation
. Let's assume I've a simple JPA entity, by the way it is convertible with a related DTO it doesn't matter.
@Entity
public class User {
...
private String email;
private String password;
...
}
I've a RESTful controller with two different routes, a secured one and a public one.
@RestController
public class UserController {
...
@GetMapping("/public")
private User publicRoute() {
return service.getLatestUser();
}
@Secured("...")
@GetMapping("/private")
private User privateRoute() {
return service.getLatestUser();
}
}
For both routes the same entity is returned, but in the first case a public representation, let's say for a user profile, without sensitive stuff like E-Mail and Password should be returned. However in the second case a private representation, let's say for the owner itself, is required.
Is there any elegant way for doing this? I tried it on JSON level with @JsonIgnore
but it doesn't worked for me. Also I tried to use Response
-Objects, but it results in a lot of boilerplate code! Any suggestions?
See Also: Recommended by Ananthapadmanabhan there already exists some questions/resources about this topic: