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

JavaSpringSpring Mvc

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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)

Solution 3 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFlavioView Question on Stackoverflow
Solution 1 - Javacliff.meyersView Answer on Stackoverflow
Solution 2 - JavaZiaullhaq SavanurView Answer on Stackoverflow
Solution 3 - JavaAlexView Answer on Stackoverflow