HttpEntity

public class HttpEntity<T>extends Object

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

HttpEntity 表明一個request請求或者響應,包含header和body,就包含頭和體。java

Typically used in combination with the RestTemplate, like so:web

 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

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.api

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

oride

 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:post

 @RequestMapping("/handle")
 public HttpEntity<String> handle() {
   HttpHeaders responseHeaders = new HttpHeaders();
   responseHeaders.set("MyResponseHeader", "MyValue");
   return new HttpEntity<String>("Hello World", responseHeaders);
 }
相關文章
相關標籤/搜索