org.springframework.http

Class RequestEntity<T>

  • Type Parameters:
    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("http://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 UriTemplate:

     URI uri = new UriTemplate("http://example.com/{foo}").expand("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();
     }
     
    Since:
    4.1
    Author:
    Arjen Poutsma, Sebastien Deleuze
    See Also:
    getMethod(), getUrl()
    • Constructor Detail

      • RequestEntity

        public RequestEntity(HttpMethod method,
                             java.net.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,
                             java.net.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,
                             java.net.URI url,
                             java.lang.reflect.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<java.lang.String,java.lang.String> headers,
                             HttpMethod method,
                             java.net.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<java.lang.String,java.lang.String> headers,
                             @Nullable
                             HttpMethod method,
                             java.net.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<java.lang.String,java.lang.String> headers,
                             @Nullable
                             HttpMethod method,
                             java.net.URI url,
                             @Nullable
                             java.lang.reflect.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 Detail

      • getMethod

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

        public java.net.URI getUrl()
        Return the URL of the request.
        Returns:
        the URL as a URI
      • getType

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

        public static RequestEntity.BodyBuilder method(HttpMethod method,
                                                       java.net.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
      • get

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

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

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

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

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

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

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