类 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 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"); }- 从以下版本开始:
- 3.0.2
- 作者:
- Arjen Poutsma, Brian Clozel
- 另请参阅:
getStatusCode()
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.
字段概要
从类继承的字段 org.springframework.http.HttpEntity
EMPTY
构造器概要
构造器 构造器 说明 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.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 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.从类继承的方法 org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
构造器详细资料
ResponseEntity
public ResponseEntity(HttpStatus status)
Create a newResponseEntitywith the given status code, and no body nor headers.- 参数:
status- the status code
ResponseEntity
public ResponseEntity(T body, HttpStatus status)
Create a newResponseEntitywith 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 newHttpEntitywith the given headers and status code, and no body.- 参数:
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.- 参数:
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
equals
public boolean equals(Object other)
- 覆盖:
equals在类中HttpEntity<T>
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 aResponseEntitywith the given body and the status set to OK.- 返回:
- the created
ResponseEntity - 从以下版本开始:
- 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.- 参数:
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