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 ofHttpEntitythat adds aHttpStatusstatus code. Used inRestTemplateas well@Controllermethods.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 interfaceResponseEntity.BodyBuilderDefines a builder that adds a body to the response entity.static interfaceResponseEntity.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 newResponseEntitywith the given status code, and no body nor headers.ResponseEntity(MultiValueMap<String,String> headers, HttpStatus status)Create a newHttpEntitywith the given headers and status code, and no body.ResponseEntity(T body, HttpStatus status)Create a newResponseEntitywith the given body and status code, and no headers.ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus status)Create a newHttpEntitywith the given body, headers, and status code.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ResponseEntity.BodyBuilderaccepted()Create a builder with an ACCEPTED status.static ResponseEntity.BodyBuilderbadRequest()Create a builder with a BAD_REQUEST status.static ResponseEntity.BodyBuildercreated(URI location)Create a new builder with a CREATED status and a location header set to the given URI.booleanequals(Object other)HttpStatusgetStatusCode()Return the HTTP status code of the response.intgetStatusCodeValue()Return the HTTP status code of the response.inthashCode()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.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.StringtoString()static ResponseEntity.BodyBuilderunprocessableEntity()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 newResponseEntitywith the given status code, and no body nor headers.- Parameters:
status- the status code
ResponseEntity
public ResponseEntity(T body, HttpStatus status)
Create a newResponseEntitywith 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 newHttpEntitywith 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 newHttpEntitywith 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:
equalsin classHttpEntity<T>
hashCode
public int hashCode()
- Overrides:
hashCodein classHttpEntity<T>
toString
public String toString()
- Overrides:
toStringin 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 aResponseEntitywith 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