I am using Spring version 4 (Spring data), I want to return Object as JSON, I am surprised that the following code worked even without annotating the user class with xmlRootElement:
@RequestMapping(value = "/resources/users", method = RequestMethod.GET)
public ResponseEntity<User> getUserByLogonId(OAuth2Authentication auth) {
String userLogonId = ((org.springframework.security.core.userdetails.User) auth.getUserAuthentication()
.getPrincipal()).getUsername();
UsersServices usersServices = new UsersServicesImpl(usersOperations);
User user = usersServices.findByLogonId(userLogonId);
HttpStatus userStatus = HttpStatus.NOT_FOUND;
if (user != null) {
userStatus = HttpStatus.FOUND;
}
return new ResponseEntity<User>(user, userStatus);
}
Can anyone explain? Is ResponseBody / ResponseEntity the job itself? when I need to annotate an object class that should be returned as JSON.
source
share