Class ResponseEntity<T>

java.lang.Object
org.springframework.http.HttpEntity<T>
org.springframework.http.ResponseEntity<T>
Type Parameters:
T - the body type

public class ResponseEntity<T> extends HttpEntity<T>
Extension of HttpEntity that adds an HttpStatusCode status code. Used in RestTemplate as well as in @Controller methods.

In RestTemplate, this class is returned by getForEntity() and exchange():

 ResponseEntity<String> entity = template.getForEntity("https://example.com", String.class);
 String body = entity.getBody();
 MediaType contentType = entity.getHeaders().getContentType();
 HttpStatus statusCode = entity.getStatusCode();
 

This can also be used in Spring MVC as the return value from an @Controller method:

 @RequestMapping("/handle")
 public ResponseEntity<String> handle() {
   URI location = ...;
   HttpHeaders responseHeaders = new HttpHeaders();
   responseHeaders.setLocation(location);
   responseHeaders.set("MyResponseHeader", "MyValue");
   return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
 }
 
Or, by using a builder accessible via static methods:
 @RequestMapping("/handle")
 public ResponseEntity<String> handle() {
   URI location = ...;
   return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World");
 }
 
Since:
3.0.2
Author:
Arjen Poutsma, Brian Clozel
See Also:
  • Constructor Details

    • ResponseEntity

      public ResponseEntity(HttpStatusCode status)
      Create a ResponseEntity with a status code only.
      Parameters:
      status - the status code
    • ResponseEntity

      public ResponseEntity(@Nullable T body, HttpStatusCode status)
      Create a ResponseEntity with a body and status code.
      Parameters:
      body - the entity body
      status - the status code
    • ResponseEntity

      public ResponseEntity(MultiValueMap<String,String> headers, HttpStatusCode status)
      Create a ResponseEntity with headers and a status code.
      Parameters:
      headers - the entity headers
      status - the status code
    • ResponseEntity

      public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String,String> headers, HttpStatusCode status)
      Create a ResponseEntity with a body, headers, and a status code.
      Parameters:
      body - the entity body
      headers - the entity headers
      status - the status code
    • ResponseEntity

      public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String,String> headers, int rawStatus)
      Create a ResponseEntity with a body, headers, and a raw status code.
      Parameters:
      body - the entity body
      headers - the entity headers
      rawStatus - the status code value
      Since:
      5.3.2
  • Method Details