类 RequestEntity<T>

  • 类型参数:
    T - the body type

    public class RequestEntity<T>
    extends HttpEntity<T>
    Extension of HttpEntity that adds a method and uri. Used in RestTemplate and @Controller methods.

    In RestTemplate, this class is used as parameter in exchange():

     MyRequest body = ...
     RequestEntity<MyRequest> request = RequestEntity
         .post(new URI("https://example.com/bar"))
         .accept(MediaType.APPLICATION_JSON)
         .body(body);
     ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
     

    If you would like to provide a URI template with variables, consider using DefaultUriBuilderFactory:

     // Create shared factory
     UriBuilderFactory factory = new DefaultUriBuilderFactory();
    
     // Use factory to create URL from template
     URI uri = factory.uriString("https://example.com/{foo}").build("bar");
     RequestEntity<MyRequest> request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
     

    Can also be used in Spring MVC, as a parameter in a @Controller method:

     @RequestMapping("/handle")
     public void handle(RequestEntity<String> request) {
       HttpMethod method = request.getMethod();
       URI url = request.getUrl();
       String body = request.getBody();
     }
     
    从以下版本开始:
    4.1
    作者:
    Arjen Poutsma, Sebastien Deleuze
    另请参阅:
    getMethod(), getUrl(), RestOperations.exchange(RequestEntity, Class), ResponseEntity