Package org.springframework.http
Class ResponseEntity<T>
java.lang.Object
org.springframework.http.HttpEntity<T>
org.springframework.http.ResponseEntity<T>
- Type Parameters:
- T- the body type
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, Sebastien Deleuze
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic interfaceDefines a builder that adds a body to the response entity.static interfaceDefines a builder that adds headers to the response entity.
- 
Field SummaryFields inherited from class org.springframework.http.HttpEntityEMPTY
- 
Constructor SummaryConstructorsConstructorDescriptionResponseEntity(HttpStatusCode status) Create aResponseEntitywith a status code only.ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntitywith headers and a status code.ResponseEntity(T body, HttpStatusCode status) Create aResponseEntitywith a body and status code.ResponseEntity(T body, MultiValueMap<String, String> headers, int rawStatus) Create aResponseEntitywith a body, headers, and a raw status code.ResponseEntity(T body, MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntitywith a body, headers, and a status code.
- 
Method SummaryModifier and TypeMethodDescriptionstatic ResponseEntity.BodyBuilderaccepted()Create a builder with an ACCEPTED status.static ResponseEntity.BodyBuilderCreate a builder with a BAD_REQUEST status.static ResponseEntity.BodyBuilderCreate a new builder with a CREATED status and a location header set to the given URI.booleanReturn the HTTP status code of the response.intDeprecated.inthashCode()static ResponseEntity.BodyBuilderCreate a builder with an INTERNAL_SERVER_ERROR status.static ResponseEntity.HeadersBuilder<?>Create a builder with a NO_CONTENT status.static ResponseEntity.HeadersBuilder<?>notFound()Create a builder with a NOT_FOUND status.static <T> ResponseEntity<T>A shortcut for creating aResponseEntitywith the given body and the OK status, or an empty body and a NOT FOUND status in case of an Optional.empty() parameter.static ResponseEntity.HeadersBuilder<?>of(ProblemDetail body) Create a newResponseEntity.HeadersBuilderwith its status set toProblemDetail.getStatus()and its body is set toProblemDetail.static <T> ResponseEntity<T>ofNullable(T body) static ResponseEntity.BodyBuilderok()Create a builder with the status set to OK.static <T> ResponseEntity<T>ok(T body) A shortcut for creating aResponseEntitywith the given body and the status set to OK.static ResponseEntity.BodyBuilderstatus(int status) Create a builder with the given status.static ResponseEntity.BodyBuilderstatus(HttpStatusCode status) Create a builder with the given status.toString()static ResponseEntity.BodyBuilderCreate a builder with an UNPROCESSABLE_ENTITY status.Methods inherited from class org.springframework.http.HttpEntitygetBody, getHeaders, hasBody
- 
Constructor Details- 
ResponseEntityCreate aResponseEntitywith a status code only.- Parameters:
- status- the status code
 
- 
ResponseEntityCreate aResponseEntitywith a body and status code.- Parameters:
- body- the entity body
- status- the status code
 
- 
ResponseEntityCreate aResponseEntitywith headers and a status code.- Parameters:
- headers- the entity headers
- status- the status code
 
- 
ResponseEntitypublic ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntitywith a body, headers, and a status code.- Parameters:
- body- the entity body
- headers- the entity headers
- status- the status code
 
- 
ResponseEntitypublic ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) Create aResponseEntitywith 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- 
getStatusCodeReturn the HTTP status code of the response.- Returns:
- the HTTP status as an HttpStatus enum entry
 
- 
getStatusCodeValueDeprecated.as of 6.0, in favor ofgetStatusCode()Return the HTTP status code of the response.- Returns:
- the HTTP status as an int value
- Since:
- 4.3
 
- 
equals- Overrides:
- equalsin class- HttpEntity<T>
 
- 
hashCodepublic int hashCode()- Overrides:
- hashCodein class- HttpEntity<T>
 
- 
toString- Overrides:
- toStringin class- HttpEntity<T>
 
- 
statusCreate a builder with the given status.- Parameters:
- status- the response status
- Returns:
- the created builder
- Since:
- 4.1
 
- 
statusCreate a builder with the given status.- Parameters:
- status- the response status
- Returns:
- the created builder
- Since:
- 4.1
 
- 
okCreate a builder with the status set to OK.- Returns:
- the created builder
- Since:
- 4.1
 
- 
okA shortcut for creating aResponseEntitywith 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
 
- 
ofA shortcut for creating aResponseEntitywith 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
 
- 
ofCreate a newResponseEntity.HeadersBuilderwith its status set toProblemDetail.getStatus()and its body is set toProblemDetail.Note: If there are no headers to add, there is usually no need to create a ResponseEntitysinceProblemDetailis also supported as a return value from controller methods.- Parameters:
- body- the problem detail to use
- Returns:
- the created builder
- Since:
- 6.0
 
- 
ofNullableA shortcut for creating aResponseEntitywith the given body and the OK status, or an empty body and a NOT FOUND status in case of anullparameter.- Returns:
- the created ResponseEntity
- Since:
- 6.0.5
 
- 
createdCreate 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
 
- 
acceptedCreate a builder with an ACCEPTED status.- Returns:
- the created builder
- Since:
- 4.1
 
- 
noContentCreate a builder with a NO_CONTENT status.- Returns:
- the created builder
- Since:
- 4.1
 
- 
badRequestCreate a builder with a BAD_REQUEST status.- Returns:
- the created builder
- Since:
- 4.1
 
- 
notFoundCreate a builder with a NOT_FOUND status.- Returns:
- the created builder
- Since:
- 4.1
 
- 
unprocessableEntityCreate a builder with an UNPROCESSABLE_ENTITY status.- Returns:
- the created builder
- Since:
- 4.1.3
 
- 
internalServerErrorCreate a builder with an INTERNAL_SERVER_ERROR status.- Returns:
- the created builder
- Since:
- 5.3.8
 
 
- 
getStatusCode()