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 HttpEntityEMPTY
- 
Constructor SummaryConstructorsConstructorDescriptionResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, int rawStatus) Create aResponseEntitywith a body, headers, and a raw status code.ResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, HttpStatusCode statusCode) Create aResponseEntitywith a body, headers, and a status code.ResponseEntity(@Nullable T body, HttpStatusCode status) Create aResponseEntitywith a body and status code.ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) Deprecated, for removal: This API element is subject to removal in a future version.ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode statusCode) Deprecated, for removal: This API element is subject to removal in a future version.ResponseEntity(HttpHeaders headers, HttpStatusCode status) Create aResponseEntitywith headers and a status code.ResponseEntity(HttpStatusCode status) Create aResponseEntitywith a status code only.ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode status) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofResponseEntity(HttpHeaders, HttpStatusCode)
- 
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.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(@Nullable T body) static ResponseEntity.BodyBuilderok()Create a builder with the status set to OK.static <T> ResponseEntity<T> 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_CONTENT status.static ResponseEntity.BodyBuilderDeprecated.since 7.0 in favor ofunprocessableContent()Methods inherited from class 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
- Since:
- 7.0
 
- 
ResponseEntityCreate aResponseEntitywith a body, headers, and a raw status code.- Parameters:
- body- the entity body
- headers- the entity headers
- rawStatus- the status code value
- Since:
- 7.0
 
- 
ResponseEntityCreate aResponseEntitywith a body, headers, and a status code.- Parameters:
- body- the entity body
- headers- the entity headers
- statusCode- the status code
- Since:
- 7.0
 
- 
ResponseEntity@Deprecated(since="7.0", forRemoval=true) public ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode status) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofResponseEntity(HttpHeaders, HttpStatusCode)Create aResponseEntitywith headers and a status code.- Parameters:
- headers- the entity headers
- status- the status code
 
- 
ResponseEntity@Deprecated(since="7.0", forRemoval=true) public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofResponseEntity(Object, HttpHeaders, int)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
 
- 
ResponseEntity@Deprecated(since="7.0", forRemoval=true) public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode statusCode) Deprecated, for removal: This API element is subject to removal in a future version.Create aResponseEntitywith a body, headers, and a status code.- Parameters:
- body- the entity body
- headers- the entity headers
- statusCode- the status code
 
 
- 
- 
Method Details- 
getStatusCodeReturn the HTTP status code of the response.- Returns:
- the HTTP status as an HttpStatus enum entry
 
- 
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
 
- 
ofNullable
- 
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
 
- 
unprocessableContentCreate a builder with an UNPROCESSABLE_CONTENT status.- Returns:
- the created builder
- Since:
- 7.0
 
- 
unprocessableEntityDeprecated.since 7.0 in favor ofunprocessableContent()Create 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
 
 
- 
ResponseEntity(Object, HttpHeaders, int)