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 Summary
Nested 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 Summary
Fields inherited from class HttpEntity
EMPTY -
Constructor Summary
ConstructorsConstructorDescriptionResponseEntity(@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 Summary
Modifier 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 HttpEntity
getBody, getHeaders, hasBody
-
Constructor Details
-
ResponseEntity
Create aResponseEntitywith a status code only.- Parameters:
status- the status code
-
ResponseEntity
Create aResponseEntitywith a body and status code.- Parameters:
body- the entity bodystatus- the status code
-
ResponseEntity
Create aResponseEntitywith headers and a status code.- Parameters:
headers- the entity headersstatus- the status code- Since:
- 7.0
-
ResponseEntity
Create aResponseEntitywith a body, headers, and a raw status code.- Parameters:
body- the entity bodyheaders- the entity headersrawStatus- the status code value- Since:
- 7.0
-
ResponseEntity
Create aResponseEntitywith a body, headers, and a status code.- Parameters:
body- the entity bodyheaders- the entity headersstatusCode- 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 headersstatus- 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 bodyheaders- the entity headersrawStatus- 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 bodyheaders- the entity headersstatusCode- the status code
-
-
Method Details
-
getStatusCode
Return the HTTP status code of the response.- Returns:
- the HTTP status as an HttpStatus enum entry
-
equals
- Overrides:
equalsin classHttpEntity<T>
-
hashCode
public int hashCode()- Overrides:
hashCodein classHttpEntity<T>
-
toString
- Overrides:
toStringin classHttpEntity<T>
-
status
Create a builder with the given status.- Parameters:
status- the response status- Returns:
- the created builder
- Since:
- 4.1
-
status
Create a builder with the given status.- Parameters:
status- the response status- Returns:
- the created builder
- Since:
- 4.1
-
ok
Create a builder with the status set to OK.- Returns:
- the created builder
- Since:
- 4.1
-
ok
A 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
-
of
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.- Returns:
- the created
ResponseEntity - Since:
- 5.1
-
of
Create 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
-
created
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
Create a builder with an ACCEPTED status.- Returns:
- the created builder
- Since:
- 4.1
-
noContent
Create a builder with a NO_CONTENT status.- Returns:
- the created builder
- Since:
- 4.1
-
badRequest
Create a builder with a BAD_REQUEST status.- Returns:
- the created builder
- Since:
- 4.1
-
notFound
Create a builder with a NOT_FOUND status.- Returns:
- the created builder
- Since:
- 4.1
-
unprocessableContent
Create a builder with an UNPROCESSABLE_CONTENT status.- Returns:
- the created builder
- Since:
- 7.0
-
unprocessableEntity
Deprecated.since 7.0 in favor ofunprocessableContent()Create a builder with an UNPROCESSABLE_ENTITY status.- Returns:
- the created builder
- Since:
- 4.1.3
-
internalServerError
Create a builder with an INTERNAL_SERVER_ERROR status.- Returns:
- the created builder
- Since:
- 5.3.8
-
ResponseEntity(Object, HttpHeaders, int)