Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ResponseEntity<T> and @ResponseBody?

I have a simple handler in my controller which returns a message

@RequestMapping(value = "/message") @ResponseBody public Message get() {     return new Message(penguinCounter.incrementAndGet() + " penguin!"); } 

At the same time I can use something like this

@RequestMapping(value = "/message") ResponseEntity<Message> get() {     Message message = new Message(penguinCounter.incrementAndGet() + " penguin!");     return new ResponseEntity<Message>(message, HttpStatus.OK); } 

What is the difference betweet this two approaches? Let's not take into account HttpStatus :)

like image 681
Flavio Avatar asked Mar 28 '14 23:03

Flavio


People also ask

What is the purpose of the @ResponseBody?

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

What does ResponseEntity <?> Mean?

ResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response. If we want to use it, we have to return it from the endpoint; Spring takes care of the rest. ResponseEntity is a generic type.

What is @RequestBody in Spring boot?

The @RequestBody annotation is applicable to handler methods of Spring controllers. This annotation indicates that Spring should deserialize a request body into an object. This object is passed as a handler method parameter.


2 Answers

ResponseEntity will give you some added flexibility in defining arbitrary HTTP response headers. See the 4th constructor here:

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/ResponseEntity.html

ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)  

A List of possible HTTP response headers is available here:

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses

Some commonly-used ones are Status, Content-Type and Cache-Control.

If you don't need that, using @ResponseBody will be a tiny bit more concise.

like image 178
cliff.meyers Avatar answered Oct 12 '22 10:10

cliff.meyers


HttpEntity represents an HTTP request or response consists of headers and body.

// Only talks about body & headers, but doesn't talk about status code public HttpEntity(T body, MultiValueMap<String,String> headers) 

ResponseEntity extends HttpEntity but also adds a Http status code.

// i.e ResponseEntity = HttpEntity + StatusCode public ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode) 

Hence used to fully configure the HTTP response.

For Ex:

@ControllerAdvice  public class JavaWebExeptionHandler {      @Autowired     ExceptionErrorCodeMap exceptionErrorCodeMap;      @ExceptionHandler(RuntimeException.class)     public final ResponseEntity<ExceptionResponseBody> handleAllExceptions(Exception ex) {         Integer expCode = exceptionErrorCodeMap.getExpCode(ex.getClass());         // We have not added headers to response here, If you want you can add by using respective constructor         return new ResponseEntity<ExceptionResponseBody>(new ExceptionResponseBody(expCode, ex.getMessage()),                 HttpStatus.valueOf(expCode));     }  } 

@ResponseBody indicates that return value of method on which it is used is bound to the response body (Mean the return value of method is treated as Http response body)

like image 24
Ziaullhaq Savanur Avatar answered Oct 12 '22 08:10

Ziaullhaq Savanur