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() { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("MyResponseHeader", "MyValue"); return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED); }
getStatusCode()
EMPTY
Constructor and Description |
---|
ResponseEntity(HttpStatus statusCode)
Create a new
ResponseEntity with the given status code, and no body nor headers. |
ResponseEntity(MultiValueMap<String,String> headers,
HttpStatus statusCode)
Create a new
HttpEntity with the given headers and status code, and no body. |
ResponseEntity(T body,
HttpStatus statusCode)
Create a new
ResponseEntity with the given body and status code, and no headers. |
ResponseEntity(T body,
MultiValueMap<String,String> headers,
HttpStatus statusCode)
Create a new
HttpEntity with the given body, headers, and status code. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other) |
HttpStatus |
getStatusCode()
Return the HTTP status code of the response.
|
int |
hashCode() |
String |
toString() |
getBody, getHeaders, hasBody
public ResponseEntity(HttpStatus statusCode)
ResponseEntity
with the given status code, and no body nor headers.statusCode
- the status codepublic ResponseEntity(T body, HttpStatus statusCode)
ResponseEntity
with the given body and status code, and no headers.body
- the entity bodystatusCode
- the status codepublic ResponseEntity(MultiValueMap<String,String> headers, HttpStatus statusCode)
HttpEntity
with the given headers and status code, and no body.headers
- the entity headersstatusCode
- the status codepublic ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)
HttpEntity
with the given body, headers, and status code.body
- the entity bodyheaders
- the entity headersstatusCode
- the status codepublic HttpStatus getStatusCode()
public boolean equals(Object other)
equals
in class HttpEntity<T>
public int hashCode()
hashCode
in class HttpEntity<T>
public String toString()
toString
in class HttpEntity<T>