@ResponseBody , ResponseEntity Spring return Object as JSON

Viewed 26970

I'm using Spring version 4 ( spring data), I want to return Object as JSON , I wonderd that the following code worked even without annotating the user class wtih 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 any body explain ? is ResponseBody/ResponseEntity do the work itself ? when I need to annotate the object class to be returned as JSON.

3 Answers

Spring boot uses the jackson json libraries for marshalling and unmarshalling the java objects to/from json objects no need any explicit configuration.

Related