Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ResponseBody , ResponseEntity Spring return Object as JSON

Tags:

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.

like image 264
Mohammad Karmi Avatar asked Aug 03 '17 17:08

Mohammad Karmi


People also ask

How do I return a response body in JSON?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

How does Spring boot convert object to JSON?

When Jackson is on the classpath an ObjectMapper bean is automatically configured. The spring-boot-starter-json is pulled with the spring-boot-starter-web . In Spring objects are automatically convered to JSON with the Jackson library. Spring can be configured to convert to XML as well.

How do I return a custom JSON response in Spring boot?

Steps. Please create a new package with the name 'response' in which we will create a class with the name “ResponseHandler”. This class will later be used to generate responses, where the response will be received in the form of an object with 3 parameters/values ​​in it.


1 Answers

@RestController itself adds @ResponseBody annotation. You can see it in the Github Issue

You can also check the official spring tutorials. Here you can check the below lines and examples:

These controller methods return simple POJOs - Collection<Bookmark>, and Bookmark, etc., in all but the add case. When an HTTP request comes in that specifies an Accept header, Spring MVC loops through the configured HttpMessageConverter until it finds one that can convert from the POJO domain model types into the content-type specified in the Accept header, if so configured.

You can also follow the below lines and examples from Official doc

@RestController is a stereotype annotation that combines @ResponseBody and @Controller. More than that, it gives more meaning to your Controller and also may carry additional semantics in future releases of the framework.

And,

As with @RequestBody and @ResponseBody, Spring uses HttpMessageConverter to convert from and to the request and response streams.

like image 70
sunkuet02 Avatar answered Oct 13 '22 18:10

sunkuet02