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
public class ResponseEntity<T> extends HttpEntity<T>
Extension ofHttpEntity
that adds aHttpStatus
status code. Used inRestTemplate
as well@Controller
methods.In
RestTemplate
, this class is returned bygetForEntity()
andexchange()
:ResponseEntity<String> entity = template.getForEntity("https://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"); }
- Since:
- 3.0.2
- Author:
- Arjen Poutsma, Brian Clozel
- See Also:
getStatusCode()
Nested Class Summary
Nested Classes Modifier and Type Class 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.
Field Summary
Fields inherited from class org.springframework.http.HttpEntity
EMPTY
Constructor Summary
Constructors Constructor Description ResponseEntity(HttpStatus status)
Create a newResponseEntity
with the given status code, and no body nor headers.ResponseEntity(MultiValueMap<String,String> headers, HttpStatus status)
Create a newHttpEntity
with the given headers and status code, and no body.ResponseEntity(T body, HttpStatus status)
Create a newResponseEntity
with the given body and status code, and no headers.ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus status)
Create a newHttpEntity
with the given body, headers, and status code.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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(URI location)
Create a new builder with a CREATED status and a location header set to the given URI.boolean
equals(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 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(HttpStatus status)
Create a builder with the given status.String
toString()
static ResponseEntity.BodyBuilder
unprocessableEntity()
Create a builder with an UNPROCESSABLE_ENTITY status.Methods inherited from class org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
Constructor Detail
ResponseEntity
public ResponseEntity(HttpStatus status)
Create a newResponseEntity
with the given status code, and no body nor headers.- Parameters:
status
- the status code
ResponseEntity
public ResponseEntity(T body, HttpStatus status)
Create a newResponseEntity
with the given body and status code, and no headers.- Parameters:
body
- the entity bodystatus
- the status code
ResponseEntity
public ResponseEntity(MultiValueMap<String,String> headers, HttpStatus status)
Create a newHttpEntity
with the given headers and status code, and no body.- Parameters:
headers
- the entity headersstatus
- the status code
ResponseEntity
public ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus status)
Create a newHttpEntity
with the given body, headers, and status code.- Parameters:
body
- the entity bodyheaders
- the entity headersstatus
- the status code
Method Detail
getStatusCode
public HttpStatus 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
public boolean equals(Object other)
- Overrides:
equals
in classHttpEntity<T>
hashCode
public int hashCode()
- Overrides:
hashCode
in classHttpEntity<T>
toString
public String toString()
- Overrides:
toString
in classHttpEntity<T>
status
public static ResponseEntity.BodyBuilder status(HttpStatus status)
Create a builder with the given status.- Parameters:
status
- the response status- Returns:
- the created builder
- Since:
- 4.1
status
public static ResponseEntity.BodyBuilder status(int status)
Create a builder with the given status.- Parameters:
status
- the response status- Returns:
- the created builder
- Since:
- 4.1
ok
public static ResponseEntity.BodyBuilder ok()
Create a builder with the status set to OK.- Returns:
- the created builder
- Since:
- 4.1
ok
public static <T> ResponseEntity<T> ok(T body)
A shortcut for creating aResponseEntity
with the given body and the status set to OK.- Returns:
- the created
ResponseEntity
- Since:
- 4.1
created
public static ResponseEntity.BodyBuilder created(URI location)
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
public static ResponseEntity.BodyBuilder accepted()
Create a builder with an ACCEPTED status.- Returns:
- the created builder
- Since:
- 4.1
noContent
public static ResponseEntity.HeadersBuilder<?> noContent()
Create a builder with a NO_CONTENT status.- Returns:
- the created builder
- Since:
- 4.1
badRequest
public static ResponseEntity.BodyBuilder badRequest()
Create a builder with a BAD_REQUEST status.- Returns:
- the created builder
- Since:
- 4.1
notFound
public static ResponseEntity.HeadersBuilder<?> notFound()
Create a builder with a NOT_FOUND status.- Returns:
- the created builder
- Since:
- 4.1
unprocessableEntity
public static ResponseEntity.BodyBuilder unprocessableEntity()
Create a builder with an UNPROCESSABLE_ENTITY status.- Returns:
- the created builder
- Since:
- 4.1.3