Class RequestEntity<T>

java.lang.Object
org.springframework.http.HttpEntity<T>
org.springframework.http.RequestEntity<T>
Type Parameters:
T - the body type
Direct Known Subclasses:
RequestEntity.UriTemplateRequestEntity

public class RequestEntity<T> extends HttpEntity<T>
Extension of HttpEntity that also exposes the HTTP method and the target URL. For use in the RestTemplate to prepare requests with and in @Controller methods to represent request input.

Example use with the RestTemplate:

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

Example use in an @Controller:

 @RequestMapping("/handle")
 public void handle(RequestEntity<String> request) {
   HttpMethod method = request.getMethod();
   URI url = request.getUrl();
   String body = request.getBody();
 }
 
Since:
4.1
Author:
Arjen Poutsma, Sebastien Deleuze, Parviz Rozikov
See Also:
  • Constructor Details

  • Method Details

    • getMethod

      public @Nullable HttpMethod getMethod()
      Return the HTTP method of the request.
      Returns:
      the HTTP method as an HttpMethod enum value
    • getUrl

      public URI getUrl()
      Return the URI for the target HTTP endpoint.

      Note: This method raises UnsupportedOperationException if the RequestEntity was created with a URI template and variables rather than with a URI instance. This is because a URI cannot be created without further input on how to expand template and encode the URI. In such cases, the URI is prepared by the RestTemplate with the help of the UriTemplateHandler it is configured with.

    • getType

      public @Nullable Type getType()
      Return the type of the request's body.
      Returns:
      the request's body type, or null if not known
      Since:
      4.3
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class HttpEntity<T>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class HttpEntity<T>
    • toString

      public String toString()
      Overrides:
      toString in class HttpEntity<T>
    • method

      public static RequestEntity.BodyBuilder method(HttpMethod method, URI url)
      Create a builder with the given method and url.
      Parameters:
      method - the HTTP method (GET, POST, etc)
      url - the URL
      Returns:
      the created builder
    • method

      public static RequestEntity.BodyBuilder method(HttpMethod method, String uriTemplate, Object... uriVariables)
      Create a builder with the given HTTP method, URI template, and variables.
      Parameters:
      method - the HTTP method (GET, POST, etc)
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • method

      public static RequestEntity.BodyBuilder method(HttpMethod method, String uriTemplate, Map<String,?> uriVariables)
      Create a builder with the given HTTP method, URI template, and variables.
      Parameters:
      method - the HTTP method (GET, POST, etc)
      uriTemplate - the uri template to use
      Returns:
      the created builder
      Since:
      5.3
    • get

      public static RequestEntity.HeadersBuilder<?> get(URI url)
      Create an HTTP GET builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • get

      public static RequestEntity.HeadersBuilder<?> get(String uriTemplate, Object... uriVariables)
      Create an HTTP GET builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • head

      public static RequestEntity.HeadersBuilder<?> head(URI url)
      Create an HTTP HEAD builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • head

      public static RequestEntity.HeadersBuilder<?> head(String uriTemplate, Object... uriVariables)
      Create an HTTP HEAD builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • post

      public static RequestEntity.BodyBuilder post(URI url)
      Create an HTTP POST builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • post

      public static RequestEntity.BodyBuilder post(String uriTemplate, Object... uriVariables)
      Create an HTTP POST builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • put

      public static RequestEntity.BodyBuilder put(URI url)
      Create an HTTP PUT builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • put

      public static RequestEntity.BodyBuilder put(String uriTemplate, Object... uriVariables)
      Create an HTTP PUT builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • patch

      public static RequestEntity.BodyBuilder patch(URI url)
      Create an HTTP PATCH builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • patch

      public static RequestEntity.BodyBuilder patch(String uriTemplate, Object... uriVariables)
      Create an HTTP PATCH builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • delete

      public static RequestEntity.HeadersBuilder<?> delete(URI url)
      Create an HTTP DELETE builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • delete

      public static RequestEntity.HeadersBuilder<?> delete(String uriTemplate, Object... uriVariables)
      Create an HTTP DELETE builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3
    • options

      public static RequestEntity.HeadersBuilder<?> options(URI url)
      Creates an HTTP OPTIONS builder with the given url.
      Parameters:
      url - the URL
      Returns:
      the created builder
    • options

      public static RequestEntity.HeadersBuilder<?> options(String uriTemplate, Object... uriVariables)
      Creates an HTTP OPTIONS builder with the given string base uri template.
      Parameters:
      uriTemplate - the uri template to use
      uriVariables - variables to expand the URI template with
      Returns:
      the created builder
      Since:
      5.3