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 :)
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With