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
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Defines a builder that adds a body to the response entity.static interface
Defines a builder that adds headers to the response entity. -
Field Summary
Fields inherited from class org.springframework.http.HttpEntity
EMPTY
-
Constructor Summary
ConstructorDescriptionResponseEntity
(HttpStatusCode status) Create aResponseEntity
with a status code only.ResponseEntity
(MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntity
with headers and a status code.ResponseEntity
(T body, HttpStatusCode status) Create aResponseEntity
with a body and status code.ResponseEntity
(T body, MultiValueMap<String, String> headers, int rawStatus) Create aResponseEntity
with a body, headers, and a raw status code.ResponseEntity
(T body, MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntity
with a body, headers, and a status code. -
Method Summary
Modifier and TypeMethodDescriptionstatic ResponseEntity.BodyBuilder
accepted()
Create a builder with an ACCEPTED status.static ResponseEntity.BodyBuilder
Create a builder with a BAD_REQUEST status.static ResponseEntity.BodyBuilder
Create a new builder with a CREATED status and a location header set to the given URI.boolean
Return the HTTP status code of the response.int
Deprecated.int
hashCode()
static ResponseEntity.BodyBuilder
Create 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 aResponseEntity
with 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 builder for aResponseEntity
with the givenProblemDetail
as the body, also matching to itsstatus
.static ResponseEntity.BodyBuilder
ok()
Create a builder with the status set to OK.static <T> ResponseEntity<T>
ok
(T body) A shortcut for creating aResponseEntity
with the given body and the status set to OK.static ResponseEntity.BodyBuilder
status
(int status) Create a builder with the given status.static ResponseEntity.BodyBuilder
status
(HttpStatusCode status) Create a builder with the given status.toString()
static ResponseEntity.BodyBuilder
Create a builder with an UNPROCESSABLE_ENTITY status.Methods inherited from class org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
-
Constructor Details
-
ResponseEntity
Create aResponseEntity
with a status code only.- Parameters:
status
- the status code
-
ResponseEntity
Create aResponseEntity
with a body and status code.- Parameters:
body
- the entity bodystatus
- the status code
-
ResponseEntity
Create aResponseEntity
with headers and a status code.- Parameters:
headers
- the entity headersstatus
- the status code
-
ResponseEntity
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode status) Create aResponseEntity
with a body, headers, and a status code.- Parameters:
body
- the entity bodyheaders
- the entity headersstatus
- the status code
-
ResponseEntity
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) Create aResponseEntity
with a body, headers, and a raw status code.- Parameters:
body
- the entity bodyheaders
- the entity headersrawStatus
- the status code value- Since:
- 5.3.2
-
-
Method Details
-
getStatusCode
Return the HTTP status code of the response.- Returns:
- the HTTP status as an HttpStatus enum entry
-
getStatusCodeValue
Deprecated.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:
equals
in classHttpEntity<T>
-
hashCode
public int hashCode()- Overrides:
hashCode
in classHttpEntity<T>
-
toString
- Overrides:
toString
in 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 aResponseEntity
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
A shortcut for creating aResponseEntity
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
Create a builder for aResponseEntity
with the givenProblemDetail
as the body, also matching to itsstatus
. An@ExceptionHandler
method can use to add response headers, or otherwise it can returnProblemDetail
.- Parameters:
body
- the details for an HTTP error response- Returns:
- the created builder
- Since:
- 6.0
-
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
-
unprocessableEntity
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
-
getStatusCode()