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

    • getStatusCode

      public HttpStatusCode getStatusCode()
      Return the HTTP status code of the response.
      Returns:
      the HTTP status as an HttpStatus enum entry
    • getStatusCodeValue

      @Deprecated public int getStatusCodeValue()
      Deprecated.
      as of 6.0, in favor of getStatusCode()
      Return the HTTP status code of the response.
      Returns:
      the HTTP status as an int value
      Since:
      4.3
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class HttpEntity<T>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class HttpEntity<T>
    • toString

      public String toString()
      Overrides:
      toString in class HttpEntity<T>
    • status

      public static ResponseEntity.BodyBuilder status(HttpStatusCode status)
      Create a builder with the given status.
      Parameters:
      status - the response status
      Returns:
      the created builder
      Since:
      4.1
    • status

      public static ResponseEntity.BodyBuilder status(int status)
      Create a builder with the given status.
      Parameters:
      status - the response status
      Returns:
      the created builder
      Since:
      4.1
    • ok

      public static ResponseEntity.BodyBuilder ok()
      Create a builder with the status set to OK.
      Returns:
      the created builder
      Since:
      4.1
    • ok

      public static <T> ResponseEntity<T> ok(@Nullable T body)
      A shortcut for creating a ResponseEntity with the given body and the status set to OK.
      Parameters:
      body - the body of the response entity (possibly empty)
      Returns:
      the created ResponseEntity
      Since:
      4.1
    • of

      public static <T> ResponseEntity<T> of(Optional<T> body)
      A shortcut for creating a ResponseEntity with the given body and the OK status, or an empty body and a NOT FOUND status in case of an Optional.empty() parameter.
      Returns:
      the created ResponseEntity
      Since:
      5.1
    • of

      public static ResponseEntity.HeadersBuilder<?> of(ProblemDetail body)
      Create a builder for a ResponseEntity with the given ProblemDetail as the body, also matching to its status. An @ExceptionHandler method can use to add response headers, or otherwise it can return ProblemDetail.
      Parameters:
      body - the details for an HTTP error response
      Returns:
      the created builder
      Since:
      6.0
    • created

      public static ResponseEntity.BodyBuilder created(URI location)
      Create a new builder with a CREATED status and a location header set to the given URI.
      Parameters:
      location - the location URI
      Returns:
      the created builder
      Since:
      4.1
    • accepted

      public static ResponseEntity.BodyBuilder accepted()
      Create a builder with an ACCEPTED status.
      Returns:
      the created builder
      Since:
      4.1
    • noContent

      public static ResponseEntity.HeadersBuilder<?> noContent()
      Create a builder with a NO_CONTENT status.
      Returns:
      the created builder
      Since:
      4.1
    • badRequest

      public static ResponseEntity.BodyBuilder badRequest()
      Create a builder with a BAD_REQUEST status.
      Returns:
      the created builder
      Since:
      4.1
    • notFound

      public static ResponseEntity.HeadersBuilder<?> notFound()
      Create a builder with a NOT_FOUND status.
      Returns:
      the created builder
      Since:
      4.1
    • unprocessableEntity

      public static ResponseEntity.BodyBuilder unprocessableEntity()
      Create a builder with an UNPROCESSABLE_ENTITY status.
      Returns:
      the created builder
      Since:
      4.1.3
    • internalServerError

      public static ResponseEntity.BodyBuilder internalServerError()
      Create a builder with an INTERNAL_SERVER_ERROR status.
      Returns:
      the created builder
      Since:
      5.3.8