org.springframework.http

Class ResponseEntity<T>

  • Type Parameters:
    T - the body type


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

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

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

    Can also be used in Spring MVC, as the return value from a @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:
    getStatusCode()
    • Constructor Detail

      • ResponseEntity

        public ResponseEntity(HttpStatus status)
        Create a new ResponseEntity with the given status code, and no body nor headers.
        Parameters:
        status - the status code
      • ResponseEntity

        public ResponseEntity(@Nullable
                              T body,
                              HttpStatus status)
        Create a new ResponseEntity with the given body and status code, and no headers.
        Parameters:
        body - the entity body
        status - the status code
      • ResponseEntity

        public ResponseEntity(MultiValueMap<java.lang.String,java.lang.String> headers,
                              HttpStatus status)
        Create a new HttpEntity with the given headers and status code, and no body.
        Parameters:
        headers - the entity headers
        status - the status code
      • ResponseEntity

        public ResponseEntity(@Nullable
                              T body,
                              @Nullable
                              MultiValueMap<java.lang.String,java.lang.String> headers,
                              HttpStatus status)
        Create a new HttpEntity with the given body, headers, and status code.
        Parameters:
        body - the entity body
        headers - the entity headers
        status - the status code
    • Method Detail

      • getStatusCode

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

        public int getStatusCodeValue()
        Return the HTTP status code of the response.
        Returns:
        the HTTP status as an int value
        Since:
        4.3
      • status

        public static ResponseEntity.BodyBuilder status(HttpStatus 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
      • of

        public static <T> ResponseEntity<T> of(java.util.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 a Optional.empty() parameter.
        Returns:
        the created ResponseEntity
        Since:
        5.1
      • ok

        public static <T> ResponseEntity<T> ok(T body)
        A shortcut for creating a ResponseEntity with the given body and the status set to OK.
        Returns:
        the created ResponseEntity
        Since:
        4.1
      • created

        public static ResponseEntity.BodyBuilder created(java.net.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