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");
}
getStatusCode(), org.springframework.web.client.RestOperations#getForEntity(String, Class, Object...) , org.springframework.web.client.RestOperations#getForEntity(String, Class, java.util.Map) , org.springframework.web.client.RestOperations#getForEntity(URI, Class) , RequestEntity
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ResponseEntity.BodyBuilder
Defines a builder that adds a body to the response entity. |
| private static class | |
| public static interface | ResponseEntity.HeadersBuilder<
the builder subclass B extends ResponseEntity.Defines a builder that adds headers to the response entity. |
| Modifier and Type | Field and Description |
|---|---|
| private final HttpStatusCode |
| Access | Constructor and Description |
|---|---|
| public | ResponseEntity(HttpStatusCode
the status code status)Create a |
| public | ResponseEntity(T
the entity body body, HttpStatusCode the status code status)Create a |
| public | ResponseEntity(MultiValueMap<String, String>
the entity headers headers, HttpStatusCode the status code status)Create a |
| public | ResponseEntity(T
the entity body body, MultiValueMap<String, String> the entity headers headers, int the status code value rawStatus)Create a |
| public | ResponseEntity(T
the entity body body, MultiValueMap<String, String> the entity headers headers, HttpStatusCode the status code statusCode)Create a |
| Modifier and Type | Method and Description |
|---|---|
| public static ResponseEntity. | |
| public static ResponseEntity. | |
| public static ResponseEntity. | |
| public boolean | equals(Object
the reference object with which to compare. other)Overrides org. |
| public HttpStatusCode | Returns: the HTTP status as an HttpStatus enum entryReturn the HTTP status code of the response. |
| public int | Returns: the HTTP status as an int value
Deprecated
since 6.0. as of 6.0, in favor of
Return the HTTP status code of the response.
getStatusCode(); scheduled
for removal in 7.0
|
| public int | hashCode()
Overrides org. |
| public static ResponseEntity. | Returns: the created builderCreate a builder with an INTERNAL_SERVER_ERROR status. |
| public static ResponseEntity. | |
| public static ResponseEntity. | |
| public static <T> ResponseEntity | Returns: the createdResponseEntityA shortcut for creating a |
| public static ResponseEntity. | Returns: the created builderthe problem detail to use body)Create a new |
| public static <T> ResponseEntity | Returns: the createdResponseEntityA shortcut for creating a |
| public static ResponseEntity. | |
| public static <T> ResponseEntity | |
| public static ResponseEntity. | Returns: the created builderthe response status status)Create a builder with the given status. |
| public static ResponseEntity. | Returns: the created builderthe response status status)Create a builder with the given status. |
| public String | toString()
Overrides org. |
| public static ResponseEntity. | Returns: the created builderCreate a builder with an UNPROCESSABLE_ENTITY status. |