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

4

71

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 :)

Longwise answered 28/3, 2014 at 23:51 Comment(0)
W
72

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.

Waltraudwaltz answered 29/3, 2014 at 0:4 Comment(1)
What stuff is transmitted in header? Any examples?Longwise
L
7

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)

Leucas answered 10/8, 2019 at 5:54 Comment(0)
K
4

ResponseEntity<> is a generic class with a type parameter, you can specify what type of object to be serialized into the response body.

@ResponseBody is an annotation, indicates that the return value of a method will be serialized into the body of the HTTP response.

you can set headers using ResponseEntity<>

Knavery answered 24/10, 2021 at 22:49 Comment(0)
A
-1

@ResponseEntity represents a response which includes headers, body and status code. @ResponseBody only returns the body of the response.

Allogamy answered 8/12, 2022 at 5:59 Comment(1)
You confused me, I thought there was a @ResponseEntity annotation.Eloign

© 2022 - 2024 — McMap. All rights reserved.