public class ResponseEntity<T> extends HttpEntity<T>
HttpEntity
that adds a HttpStatus
status code.
Used in RestTemplate
as well @Controller
methods.
In RestTemplate
, this class is returned by
getForEntity()
and
exchange()
:
ResponseEntity<String> entity = template.getForEntity("http://example.com", String.class); String body = entity.getBody(); MediaType contentType = entity.getHeaders().getContentType(); HttpStatus statusCode = entity.getStatusCode();
Can also be used in Spring MVC, as the return value from a @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"); }
getStatusCode()
Modifier and Type | Class and Description |
---|---|
static interface |
ResponseEntity.BodyBuilder
Defines a builder that adds a body to the response entity.
|
static interface |
ResponseEntity.HeadersBuilder<B extends ResponseEntity.HeadersBuilder<B>>
Defines a builder that adds headers to the response entity.
|
EMPTY
Constructor and Description |
---|
ResponseEntity(HttpStatus status)
Create a new
ResponseEntity with the given status code, and no body nor headers. |
ResponseEntity(MultiValueMap<java.lang.String,java.lang.String> headers,
HttpStatus status)
Create a new
HttpEntity with the given headers and status code, and no body. |
ResponseEntity(T body,
HttpStatus status)
Create a new
ResponseEntity with the given body and status code, and no headers. |
ResponseEntity(T body,
MultiValueMap<java.lang.String,java.lang.String> headers,
HttpStatus status)
Create a new
HttpEntity with the given body, headers, and status code. |
Modifier and Type | Method and Description |
---|---|
static ResponseEntity.BodyBuilder |
accepted()
Create a builder with an ACCEPTED status.
|
static ResponseEntity.BodyBuilder |
badRequest()
Create a builder with a BAD_REQUEST status.
|
static ResponseEntity.BodyBuilder |
created(java.net.URI location)
Create a new builder with a CREATED status
and a location header set to the given URI.
|
boolean |
equals(java.lang.Object other) |
HttpStatus |
getStatusCode()
Return the HTTP status code of the response.
|
int |
getStatusCodeValue()
Return the HTTP status code of the response.
|
int |
hashCode() |
static ResponseEntity.HeadersBuilder<?> |
noContent()
Create a builder with a NO_CONTENT status.
|
static ResponseEntity.HeadersBuilder<?> |
notFound()
Create a builder with a NOT_FOUND status.
|
static ResponseEntity.BodyBuilder |
ok()
Create a builder with the status set to OK.
|
static <T> ResponseEntity<T> |
ok(T body)
A shortcut for creating a
ResponseEntity with the given body and
the status set to OK. |
static ResponseEntity.BodyBuilder |
status(HttpStatus status)
Create a builder with the given status.
|
static ResponseEntity.BodyBuilder |
status(int status)
Create a builder with the given status.
|
java.lang.String |
toString() |
static ResponseEntity.BodyBuilder |
unprocessableEntity()
Create a builder with an
UNPROCESSABLE_ENTITY status.
|
getBody, getHeaders, hasBody
public ResponseEntity(HttpStatus status)
ResponseEntity
with the given status code, and no body nor headers.status
- the status codepublic ResponseEntity(T body, HttpStatus status)
ResponseEntity
with the given body and status code, and no headers.body
- the entity bodystatus
- the status codepublic ResponseEntity(MultiValueMap<java.lang.String,java.lang.String> headers, HttpStatus status)
HttpEntity
with the given headers and status code, and no body.headers
- the entity headersstatus
- the status codepublic ResponseEntity(T body, MultiValueMap<java.lang.String,java.lang.String> headers, HttpStatus status)
HttpEntity
with the given body, headers, and status code.body
- the entity bodyheaders
- the entity headersstatus
- the status codepublic HttpStatus getStatusCode()
public int getStatusCodeValue()
public boolean equals(java.lang.Object other)
equals
in class HttpEntity<T>
public int hashCode()
hashCode
in class HttpEntity<T>
public java.lang.String toString()
toString
in class HttpEntity<T>
public static ResponseEntity.BodyBuilder status(HttpStatus status)
status
- the response statuspublic static ResponseEntity.BodyBuilder status(int status)
status
- the response statuspublic static ResponseEntity.BodyBuilder ok()
public static <T> ResponseEntity<T> ok(T body)
ResponseEntity
with the given body and
the status set to OK.ResponseEntity
public static ResponseEntity.BodyBuilder created(java.net.URI location)
location
- the location URIpublic static ResponseEntity.BodyBuilder accepted()
public static ResponseEntity.HeadersBuilder<?> noContent()
public static ResponseEntity.BodyBuilder badRequest()
public static ResponseEntity.HeadersBuilder<?> notFound()
public static ResponseEntity.BodyBuilder unprocessableEntity()