T
- the body typepublic class RequestEntity<T> extends HttpEntity<T>
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(); }
getMethod()
,
getUrl()
Modifier and Type | Class and Description |
---|---|
static interface |
RequestEntity.BodyBuilder
Defines a builder that adds a body to the response entity.
|
static interface |
RequestEntity.HeadersBuilder<B extends RequestEntity.HeadersBuilder<B>>
Defines a builder that adds headers to the request entity.
|
EMPTY
Constructor and Description |
---|
RequestEntity(HttpMethod method,
java.net.URI url)
Constructor with method and URL but without body nor headers.
|
RequestEntity(MultiValueMap<java.lang.String,java.lang.String> headers,
HttpMethod method,
java.net.URI url)
Constructor with method, URL and headers but without body.
|
RequestEntity(T body,
HttpMethod method,
java.net.URI url)
Constructor with method, URL and body but without headers.
|
RequestEntity(T body,
HttpMethod method,
java.net.URI url,
java.lang.reflect.Type type)
Constructor with method, URL, body and type but without headers.
|
RequestEntity(T body,
MultiValueMap<java.lang.String,java.lang.String> headers,
HttpMethod method,
java.net.URI url)
Constructor with method, URL, headers and body.
|
RequestEntity(T body,
MultiValueMap<java.lang.String,java.lang.String> headers,
HttpMethod method,
java.net.URI url,
java.lang.reflect.Type type)
Constructor with method, URL, headers, body and type.
|
Modifier and Type | Method and Description |
---|---|
static RequestEntity.HeadersBuilder<?> |
delete(java.net.URI url)
Create an HTTP DELETE builder with the given url.
|
boolean |
equals(java.lang.Object other) |
static RequestEntity.HeadersBuilder<?> |
get(java.net.URI url)
Create an HTTP GET builder with the given url.
|
HttpMethod |
getMethod()
Return the HTTP method of the request.
|
java.lang.reflect.Type |
getType()
Return the type of the request's body.
|
java.net.URI |
getUrl()
Return the URL of the request.
|
int |
hashCode() |
static RequestEntity.HeadersBuilder<?> |
head(java.net.URI url)
Create an HTTP HEAD builder with the given url.
|
static RequestEntity.BodyBuilder |
method(HttpMethod method,
java.net.URI url)
Create a builder with the given method and url.
|
static RequestEntity.HeadersBuilder<?> |
options(java.net.URI url)
Creates an HTTP OPTIONS builder with the given url.
|
static RequestEntity.BodyBuilder |
patch(java.net.URI url)
Create an HTTP PATCH builder with the given url.
|
static RequestEntity.BodyBuilder |
post(java.net.URI url)
Create an HTTP POST builder with the given url.
|
static RequestEntity.BodyBuilder |
put(java.net.URI url)
Create an HTTP PUT builder with the given url.
|
java.lang.String |
toString() |
getBody, getHeaders, hasBody
public RequestEntity(HttpMethod method, java.net.URI url)
method
- the methodurl
- the URLpublic RequestEntity(@Nullable T body, HttpMethod method, java.net.URI url)
body
- the bodymethod
- the methodurl
- the URLpublic RequestEntity(@Nullable T body, HttpMethod method, java.net.URI url, java.lang.reflect.Type type)
body
- the bodymethod
- the methodurl
- the URLtype
- the type used for generic type resolutionpublic RequestEntity(MultiValueMap<java.lang.String,java.lang.String> headers, HttpMethod method, java.net.URI url)
headers
- the headersmethod
- the methodurl
- the URLpublic RequestEntity(@Nullable T body, @Nullable MultiValueMap<java.lang.String,java.lang.String> headers, @Nullable HttpMethod method, java.net.URI url)
body
- the bodyheaders
- the headersmethod
- the methodurl
- the URLpublic 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)
body
- the bodyheaders
- the headersmethod
- the methodurl
- the URLtype
- the type used for generic type resolution@Nullable public HttpMethod getMethod()
HttpMethod
enum valuepublic java.net.URI getUrl()
URI
@Nullable public java.lang.reflect.Type getType()
null
if not knownpublic boolean equals(@Nullable java.lang.Object other)
equals
in class HttpEntity<T>
public int hashCode()
hashCode
in class HttpEntity<T>
public java.lang.String toString()
toString
in class HttpEntity<T>
public static RequestEntity.BodyBuilder method(HttpMethod method, java.net.URI url)
method
- the HTTP method (GET, POST, etc)url
- the URLpublic static RequestEntity.HeadersBuilder<?> get(java.net.URI url)
url
- the URLpublic static RequestEntity.HeadersBuilder<?> head(java.net.URI url)
url
- the URLpublic static RequestEntity.BodyBuilder post(java.net.URI url)
url
- the URLpublic static RequestEntity.BodyBuilder put(java.net.URI url)
url
- the URLpublic static RequestEntity.BodyBuilder patch(java.net.URI url)
url
- the URLpublic static RequestEntity.HeadersBuilder<?> delete(java.net.URI url)
url
- the URLpublic static RequestEntity.HeadersBuilder<?> options(java.net.URI url)
url
- the URL