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);
}