类 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);
     
    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()
    • 字段详细资料

      • EMPTY

        public static final HttpEntity<?> EMPTY
        The empty HttpEntity, with no body or headers.
    • 构造器详细资料

      • HttpEntity

        protected HttpEntity()
        Create a new, empty HttpEntity.
      • HttpEntity

        public HttpEntity​(T body)
        Create a new HttpEntity with the given body and no headers.
        参数:
        body - the entity body
      • HttpEntity

        public HttpEntity​(T body,
                          MultiValueMap<String,​String> headers)
        Create a new HttpEntity with the given body and headers.
        参数:
        body - the entity body
        headers - the entity headers