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 HttpStatus 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
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 org.springframework.http.HttpEntity
EMPTY -
Constructor Summary
ConstructorsConstructorDescriptionResponseEntity(HttpStatus status) Create aResponseEntitywith a status code only.ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) Create aResponseEntitywith headers and a status code.ResponseEntity(T body, HttpStatus 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, HttpStatus status) Create aResponseEntitywith a body, headers, and a status code. -
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.intReturn 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 builder for aResponseEntitywith the givenProblemDetailas the body, also matching to itsstatus.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(HttpStatus 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.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
-
ResponseEntity
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) Create aResponseEntitywith 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 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
-
-
Method Details
-
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
-
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 builder for aResponseEntitywith the givenProblemDetailas the body, also matching to itsstatus. An@ExceptionHandlermethod 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
-