类 HttpEntity<T>

  • 类型参数:
    T - the body type
    直接已知子类:
    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);
     
    or
     HttpEntity<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()