类 ResponseEntity<T>
- java.lang.Object
- org.springframework.http.HttpEntity<T>
- org.springframework.http.ResponseEntity<T>
- 类型参数:
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"); }
- 从以下版本开始:
- 3.0.2
- 作者:
- Arjen Poutsma, Brian Clozel
- 另请参阅:
getStatusCode()
,RestOperations.getForEntity(String, Class, Object...)
,RestOperations.getForEntity(String, Class, java.util.Map)
,RestOperations.getForEntity(URI, Class)
,RequestEntity
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.
字段概要
从类继承的字段 org.springframework.http.HttpEntity
EMPTY
构造器概要
构造器 构造器 说明 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.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 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 <T> ResponseEntity<T>
of(Optional<T> body)
A shortcut for creating aResponseEntity
with the given body and the OK status, or an empty body and a NOT FOUND status in case of an Optional.empty() parameter.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.从类继承的方法 org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
构造器详细资料
ResponseEntity
public ResponseEntity(HttpStatus status)
Create a newResponseEntity
with the given status code, and no body nor headers.- 参数:
status
- the status code
ResponseEntity
public ResponseEntity(@Nullable T body, HttpStatus status)
Create a newResponseEntity
with the given body and status code, and no headers.- 参数:
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.- 参数:
headers
- the entity headersstatus
- the status code
ResponseEntity
public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String,String> headers, HttpStatus status)
Create a newHttpEntity
with the given body, headers, and status code.- 参数:
body
- the entity bodyheaders
- the entity headersstatus
- the status code
方法详细资料
getStatusCode
public HttpStatus getStatusCode()
Return the HTTP status code of the response.- 返回:
- the HTTP status as an HttpStatus enum entry
getStatusCodeValue
public int getStatusCodeValue()
Return the HTTP status code of the response.- 返回:
- the HTTP status as an int value
- 从以下版本开始:
- 4.3
hashCode
public int hashCode()
- 覆盖:
hashCode
在类中HttpEntity<T>
toString
public String toString()
- 覆盖:
toString
在类中HttpEntity<T>
status
public static ResponseEntity.BodyBuilder status(HttpStatus status)
Create a builder with the given status.- 参数:
status
- the response status- 返回:
- the created builder
- 从以下版本开始:
- 4.1
status
public static ResponseEntity.BodyBuilder status(int status)
Create a builder with the given status.- 参数:
status
- the response status- 返回:
- the created builder
- 从以下版本开始:
- 4.1
ok
public static ResponseEntity.BodyBuilder ok()
Create a builder with the status set to OK.- 返回:
- the created builder
- 从以下版本开始:
- 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.- 返回:
- the created
ResponseEntity
- 从以下版本开始:
- 4.1
of
public static <T> ResponseEntity<T> of(Optional<T> body)
A shortcut for creating aResponseEntity
with the given body and the OK status, or an empty body and a NOT FOUND status in case of an Optional.empty() parameter.- 返回:
- the created
ResponseEntity
- 从以下版本开始:
- 5.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.- 参数:
location
- the location URI- 返回:
- the created builder
- 从以下版本开始:
- 4.1
accepted
public static ResponseEntity.BodyBuilder accepted()
Create a builder with an ACCEPTED status.- 返回:
- the created builder
- 从以下版本开始:
- 4.1
noContent
public static ResponseEntity.HeadersBuilder<?> noContent()
Create a builder with a NO_CONTENT status.- 返回:
- the created builder
- 从以下版本开始:
- 4.1
badRequest
public static ResponseEntity.BodyBuilder badRequest()
Create a builder with a BAD_REQUEST status.- 返回:
- the created builder
- 从以下版本开始:
- 4.1
notFound
public static ResponseEntity.HeadersBuilder<?> notFound()
Create a builder with a NOT_FOUND status.- 返回:
- the created builder
- 从以下版本开始:
- 4.1
unprocessableEntity
public static ResponseEntity.BodyBuilder unprocessableEntity()
Create a builder with an UNPROCESSABLE_ENTITY status.- 返回:
- the created builder
- 从以下版本开始:
- 4.1.3