您的位置:首页 > 理论基础 > 计算机网络

HttpEntity

2015-07-12 00:00 555 查看
摘要: 在阅读http相关工程的时候,有个httpentity 概念?它到底是干啥的呢?

public class HttpEntity<T>extends Object

Represents an HTTP request or response entity, consisting of headers and body.

HttpEntity 代表一个request请求或者响应,包含header和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("http://example.com", entity);

public class RestTemplate extends InterceptingHttpAccessorimplements RestOperations

另外对于spring RestTemplate 的说明。

Spring's central class for synchronous client-side HTTP access. It simplifies communication with HTTP servers, and enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extract results.

The main entry points of this template are the methods named after the six main HTTP methods:

or

HttpEntity<String> entity = template.getForEntity("http://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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HttpEntity