Package org.springframework.http
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
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Defines a builder that adds a body to the response entity.static interface
Defines a builder that adds headers to the request entity.static class
RequestEntity initialized with a URI template and variables instead of aURI
. -
Field Summary
Fields inherited from class org.springframework.http.HttpEntity
EMPTY
-
Constructor Summary
ConstructorDescriptionRequestEntity
(HttpMethod method, URI url) Constructor with method and URL but without body nor headers.RequestEntity
(MultiValueMap<String, String> headers, HttpMethod method, URI url) Constructor with method, URL and headers but without body.RequestEntity
(T body, HttpMethod method, URI url) Constructor with method, URL and body but without headers.RequestEntity
(T body, HttpMethod method, URI url, Type type) Constructor with method, URL, body and type but without headers.RequestEntity
(T body, MultiValueMap<String, String> headers, HttpMethod method, URI url) Constructor with method, URL, headers and body.RequestEntity
(T body, MultiValueMap<String, String> headers, HttpMethod method, URI url, Type type) Constructor with method, URL, headers, body and type. -
Method Summary
Modifier and TypeMethodDescriptionstatic RequestEntity.HeadersBuilder<?>
Create an HTTP DELETE builder with the given string base uri template.static RequestEntity.HeadersBuilder<?>
Create an HTTP DELETE builder with the given url.boolean
static RequestEntity.HeadersBuilder<?>
Create an HTTP GET builder with the given string base uri template.static RequestEntity.HeadersBuilder<?>
Create an HTTP GET builder with the given url.Return the HTTP method of the request.getType()
Return the type of the request's body.getUrl()
Return theURI
for the target HTTP endpoint.int
hashCode()
static RequestEntity.HeadersBuilder<?>
Create an HTTP HEAD builder with the given string base uri template.static RequestEntity.HeadersBuilder<?>
Create an HTTP HEAD builder with the given url.static RequestEntity.BodyBuilder
method
(HttpMethod method, String uriTemplate, Object... uriVariables) Create a builder with the given HTTP method, URI template, and variables.static RequestEntity.BodyBuilder
method
(HttpMethod method, String uriTemplate, Map<String, ?> uriVariables) Create a builder with the given HTTP method, URI template, and variables.static RequestEntity.BodyBuilder
method
(HttpMethod method, URI url) Create a builder with the given method and url.static RequestEntity.HeadersBuilder<?>
Creates an HTTP OPTIONS builder with the given string base uri template.static RequestEntity.HeadersBuilder<?>
Creates an HTTP OPTIONS builder with the given url.static RequestEntity.BodyBuilder
Create an HTTP PATCH builder with the given string base uri template.static RequestEntity.BodyBuilder
Create an HTTP PATCH builder with the given url.static RequestEntity.BodyBuilder
Create an HTTP POST builder with the given string base uri template.static RequestEntity.BodyBuilder
Create an HTTP POST builder with the given url.static RequestEntity.BodyBuilder
Create an HTTP PUT builder with the given string base uri template.static RequestEntity.BodyBuilder
Create an HTTP PUT builder with the given url.toString()
Methods inherited from class org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
-
Constructor Details
-
RequestEntity
Constructor with method and URL but without body nor headers.- Parameters:
method
- the methodurl
- the URL
-
RequestEntity
Constructor with method, URL and body but without headers.- Parameters:
body
- the bodymethod
- the methodurl
- the URL
-
RequestEntity
Constructor with method, URL, body and type but without headers.- Parameters:
body
- the bodymethod
- the methodurl
- the URLtype
- the type used for generic type resolution- Since:
- 4.3
-
RequestEntity
Constructor with method, URL and headers but without body.- Parameters:
headers
- the headersmethod
- the methodurl
- 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 bodyheaders
- the headersmethod
- the methodurl
- 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 bodyheaders
- the headersmethod
- the methodurl
- the URLtype
- the type used for generic type resolution- Since:
- 4.3
-
-
Method Details
-
getMethod
Return the HTTP method of the request.- Returns:
- the HTTP method as an
HttpMethod
enum value
-
getUrl
Return theURI
for the target HTTP endpoint.Note: This method raises
UnsupportedOperationException
if theRequestEntity
was created with a URI template and variables rather than with aURI
instance. This is because a URI cannot be created without further input on how to expand template and encode the URI. In such cases, theURI
is prepared by theRestTemplate
with the help of theUriTemplateHandler
it is configured with. -
getType
Return the type of the request's body.- Returns:
- the request's body type, or
null
if not known - Since:
- 4.3
-
equals
- Overrides:
equals
in classHttpEntity<T>
-
hashCode
public int hashCode()- Overrides:
hashCode
in classHttpEntity<T>
-
toString
- Overrides:
toString
in classHttpEntity<T>
-
method
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 useuriVariables
- 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
Create an HTTP GET builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
get
Create an HTTP GET builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
head
Create an HTTP HEAD builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
head
Create an HTTP HEAD builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
post
Create an HTTP POST builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
post
Create an HTTP POST builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
put
Create an HTTP PUT builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
put
Create an HTTP PUT builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
patch
Create an HTTP PATCH builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
patch
Create an HTTP PATCH builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
delete
Create an HTTP DELETE builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
delete
Create an HTTP DELETE builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-
options
Creates an HTTP OPTIONS builder with the given url.- Parameters:
url
- the URL- Returns:
- the created builder
-
options
Creates an HTTP OPTIONS builder with the given string base uri template.- Parameters:
uriTemplate
- the uri template to useuriVariables
- variables to expand the URI template with- Returns:
- the created builder
- Since:
- 5.3
-