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
public class RequestEntity<T> extends HttpEntity<T>
Extension ofHttpEntity
that adds a method and uri. Used inRestTemplate
and@Controller
methods.In
RestTemplate
, this class is used as parameter inexchange()
:MyRequest body = ... RequestEntity<MyRequest> request = RequestEntity .post(new URI("https://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
DefaultUriBuilderFactory
:// Create shared factory UriBuilderFactory factory = new DefaultUriBuilderFactory(); // Use factory to create URL from template URI uri = factory.uriString("https://example.com/{foo}").build("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()
,RestOperations.exchange(RequestEntity, Class)
,ResponseEntity
Nested Class Summary
Nested Classes Modifier and Type Class 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.
Field Summary
Fields inherited from class org.springframework.http.HttpEntity
EMPTY
Constructor Summary
Constructors Constructor Description RequestEntity(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RequestEntity.HeadersBuilder<?>
delete(URI url)
Create an HTTP DELETE builder with the given url.boolean
equals(Object other)
static RequestEntity.HeadersBuilder<?>
get(URI url)
Create an HTTP GET builder with the given url.HttpMethod
getMethod()
Return the HTTP method of the request.Type
getType()
Return the type of the request's body.URI
getUrl()
Return the URL of the request.int
hashCode()
static RequestEntity.HeadersBuilder<?>
head(URI url)
Create an HTTP HEAD builder with the given url.static RequestEntity.BodyBuilder
method(HttpMethod method, URI url)
Create a builder with the given method and url.static RequestEntity.HeadersBuilder<?>
options(URI url)
Creates an HTTP OPTIONS builder with the given url.static RequestEntity.BodyBuilder
patch(URI url)
Create an HTTP PATCH builder with the given url.static RequestEntity.BodyBuilder
post(URI url)
Create an HTTP POST builder with the given url.static RequestEntity.BodyBuilder
put(URI url)
Create an HTTP PUT builder with the given url.String
toString()
Methods inherited from class org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
Constructor Detail
RequestEntity
public RequestEntity(HttpMethod method, URI url)
Constructor with method and URL but without body nor headers.- Parameters:
method
- the methodurl
- the URL
RequestEntity
public RequestEntity(@Nullable T body, HttpMethod method, URI url)
Constructor with method, URL and body but without headers.- Parameters:
body
- the bodymethod
- the methodurl
- 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 bodymethod
- the methodurl
- the URLtype
- 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 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, 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 Detail
getMethod
@Nullable public HttpMethod getMethod()
Return the HTTP method of the request.- Returns:
- the HTTP method as an
HttpMethod
enum value
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
hashCode
public int hashCode()
- Overrides:
hashCode
in classHttpEntity<T>
toString
public String toString()
- Overrides:
toString
in classHttpEntity<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
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
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
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
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
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
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
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