类 HttpEntity<T>
- java.lang.Object
- org.springframework.http.HttpEntity<T>
- 直接已知子类:
RequestEntity
,ResponseEntity
public class HttpEntity<T> extends Object
Represents an HTTP request or response entity, consisting of headers and body.Typically used in combination with the
RestTemplate
, like so:HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<String>(helloWorld, headers); URI location = template.postForLocation("https://example.com", entity);
orHttpEntity<String> entity = template.getForEntity("https://example.com", String.class); String body = entity.getBody(); MediaType contentType = entity.getHeaders().getContentType();
Can also be used in Spring MVC, as a return value from a @Controller method:@RequestMapping("/handle") public HttpEntity<String> handle() { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("MyResponseHeader", "MyValue"); return new HttpEntity<String>("Hello World", responseHeaders); }
- 从以下版本开始:
- 3.0.2
- 作者:
- Arjen Poutsma, Juergen Hoeller
- 另请参阅:
RestTemplate
,getBody()
,getHeaders()
字段概要
字段 修饰符和类型 字段 说明 static HttpEntity<?>
EMPTY
The emptyHttpEntity
, with no body or headers.
构造器概要
构造器 限定符 构造器 说明 protected
HttpEntity()
Create a new, emptyHttpEntity
.HttpEntity(MultiValueMap<String,String> headers)
Create a newHttpEntity
with the given headers and no body.HttpEntity(T body)
Create a newHttpEntity
with the given body and no headers.HttpEntity(T body, MultiValueMap<String,String> headers)
Create a newHttpEntity
with the given body and headers.
字段详细资料
EMPTY
public static final HttpEntity<?> EMPTY
The emptyHttpEntity
, with no body or headers.
构造器详细资料
HttpEntity
protected HttpEntity()
Create a new, emptyHttpEntity
.
HttpEntity
public HttpEntity(T body)
Create a newHttpEntity
with the given body and no headers.- 参数:
body
- the entity body
HttpEntity
public HttpEntity(MultiValueMap<String,String> headers)
Create a newHttpEntity
with the given headers and no body.- 参数:
headers
- the entity headers
HttpEntity
public HttpEntity(T body, MultiValueMap<String,String> headers)
Create a newHttpEntity
with the given body and headers.- 参数:
body
- the entity bodyheaders
- the entity headers
方法详细资料
getHeaders
public HttpHeaders getHeaders()
Returns the headers of this entity.
hasBody
public boolean hasBody()
Indicates whether this entity has a body.