Top Description Inners Fields Constructors Methods
org.springframework.http

public Class ResponseEntity<T>

extends HttpEntity<T>
Class Inheritance
Type Parameters
<T>
the body type
Imports
java.net.URI, java.time.Instant, .ZonedDateTime, java.util.Arrays, .LinkedHashSet, .Optional, .Set, java.util.function.Consumer, org.springframework.lang.Nullable, org.springframework.util.Assert, .MultiValueMap, .ObjectUtils

Extension of 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");
}
Authors
Arjen Poutsma, Brian Clozel, Sebastien Deleuze
Since
3.0.2
See Also
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

Nested and Inner Type Summary

Modifier and TypeClass 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.HeadersBuilder<B>
>

Defines a builder that adds headers to the response entity.

Field Summary

Modifier and TypeField and Description
private final HttpStatusCode
Inherited from org.springframework.http.HttpEntity:
EMPTY

Constructor Summary

AccessConstructor and Description
public
ResponseEntity(HttpStatusCode
the status code
status
)

Create a ResponseEntity with a status code only.

public
ResponseEntity(T
the entity body
body
,
HttpStatusCode
the status code
status
)

Create a ResponseEntity with a body and status code.

public
ResponseEntity(MultiValueMap<String, String>
the entity headers
headers
,
HttpStatusCode
the status code
status
)

Create a ResponseEntity with headers and a status code.

public
ResponseEntity(T
the entity body
body
,
MultiValueMap<String, String>
the entity headers
headers
,
int
the status code value
rawStatus
)

Create a ResponseEntity with a body, headers, and a raw status code.

public
ResponseEntity(T
the entity body
body
,
MultiValueMap<String, String>
the entity headers
headers
,
HttpStatusCode
the status code
statusCode
)

Create a ResponseEntity with a body, headers, and a status code.

Method Summary

Modifier and TypeMethod and Description
public static ResponseEntity.BodyBuilder

Returns:

the created builder
accepted
()

Create a builder with an ACCEPTED status.

public static ResponseEntity.BodyBuilder

Returns:

the created builder
badRequest
()

Create a builder with a BAD_REQUEST status.

public static ResponseEntity.BodyBuilder

Returns:

the created builder
created
(URI
the location URI
location
)

Create a new builder with a CREATED status and a location header set to the given URI.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.http.HttpEntity.equals.

Indicates whether some other object is "equal to" this one.
public HttpStatusCode

Returns:

the HTTP status as an HttpStatus enum entry
getStatusCode
()

Return the HTTP status code of the response.

public int

Returns:

the HTTP status as an int value
getStatusCodeValue
()

Deprecated since 6.0. as of 6.0, in favor of getStatusCode(); scheduled for removal in 7.0
Return the HTTP status code of the response.
public int
hashCode()

Overrides org.springframework.http.HttpEntity.hashCode.

Returns a hash code value for the object.
public static ResponseEntity.BodyBuilder

Returns:

the created builder
internalServerError
()

Create a builder with an INTERNAL_SERVER_ERROR status.

public static ResponseEntity.HeadersBuilder<?>

Returns:

the created builder
noContent
()

Create a builder with a NO_CONTENT status.

public static ResponseEntity.HeadersBuilder<?>

Returns:

the created builder
notFound
()

Create a builder with a NOT_FOUND status.

public static <T> ResponseEntity<T>

Returns:

the created ResponseEntity
of
(Optional<T> body)

A shortcut for creating a ResponseEntity with the given body and the OK status, or an empty body and a NOT FOUND status in case of an Optional#empty() parameter.

public static ResponseEntity.HeadersBuilder<?>

Returns:

the created builder
of
(ProblemDetail
the problem detail to use
body
)

Create a new HeadersBuilder with its status set to ProblemDetail#getStatus() and its body is set to ProblemDetail.

public static <T> ResponseEntity<T>

Returns:

the created ResponseEntity
ofNullable
(T body)

A shortcut for creating a ResponseEntity with the given body and the OK status, or an empty body and a NOT FOUND status in case of a null parameter.

public static ResponseEntity.BodyBuilder

Returns:

the created builder
ok
()

Create a builder with the status set to OK.

public static <T> ResponseEntity<T>

Returns:

the created ResponseEntity
ok
(T
the body of the response entity (possibly empty)
body
)

A shortcut for creating a ResponseEntity with the given body and the status set to OK.

public static ResponseEntity.BodyBuilder

Returns:

the created builder
status
(HttpStatusCode
the response status
status
)

Create a builder with the given status.

public static ResponseEntity.BodyBuilder

Returns:

the created builder
status
(int
the response status
status
)

Create a builder with the given status.

public String
toString()

Overrides org.springframework.http.HttpEntity.toString.

Returns a string representation of the object.
public static ResponseEntity.BodyBuilder

Returns:

the created builder
unprocessableEntity
()

Create a builder with an UNPROCESSABLE_ENTITY status.

Inherited from org.springframework.http.HttpEntity:
getBodygetHeadershasBody