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

    • RequestEntity

      public RequestEntity(HttpMethod method, URI url)
      Constructor with method and URL but without body nor headers.
      Parameters:
      method - the method
      url - the URL
    • RequestEntity

      public RequestEntity(@Nullable T body, HttpMethod method, URI url)
      Constructor with method, URL and body but without headers.
      Parameters:
      body - the body
      method - the method
      url - the URL
    • RequestEntity

      public RequestEntity(@Nullable T body, HttpMethod method, URI url, Type type)
      Constructor with method, URL, body and type but without headers.
      Parameters:
      body - the body
      method - the method
      url - the URL
      type - the type used for generic type resolution
      Since:
      4.3
    • RequestEntity

      public RequestEntity(MultiValueMap<String,String> headers, HttpMethod method, URI url)
      Constructor with method, URL and headers but without body.
      Parameters:
      headers - the headers
      method - the method
      url - the URL
    • RequestEntity

      public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String,String> headers, @Nullable HttpMethod method, URI url)
      Constructor with method, URL, headers and body.
      Parameters:
      body - the body
      headers - the headers
      method - the method
      url - the URL
    • RequestEntity

      public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String,String> headers, @Nullable HttpMethod method, @Nullable URI url, @Nullable Type type)
      Constructor with method, URL, headers, body and type.
      Parameters:
      body - the body
      headers - the headers
      method - the method
      url - the URL
      type - the type used for generic type resolution
      Since:
      4.3
  • Method Details

    • getMethod

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

      public URI getUrl()
      Return the URL of the request.
    • getType

      @Nullable public 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