类 RequestEntity<T>
- java.lang.Object
- org.springframework.http.HttpEntity<T>
- org.springframework.http.RequestEntity<T>
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
UriTemplate
:URI uri = new UriTemplate("https://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(); }
- 从以下版本开始:
- 4.1
- 作者:
- Arjen Poutsma, Sebastien Deleuze
- 另请参阅:
getMethod()
,getUrl()
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.
字段概要
从类继承的字段 org.springframework.http.HttpEntity
EMPTY
构造器概要
构造器 构造器 说明 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.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 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()
从类继承的方法 org.springframework.http.HttpEntity
getBody, getHeaders, hasBody
构造器详细资料
RequestEntity
public RequestEntity(HttpMethod method, URI url)
Constructor with method and URL but without body nor headers.- 参数:
method
- the methodurl
- the URL
RequestEntity
public RequestEntity(T body, HttpMethod method, URI url)
Constructor with method, URL and body but without headers.- 参数:
body
- the bodymethod
- the methodurl
- the URL
RequestEntity
public RequestEntity(T body, HttpMethod method, URI url, Type type)
Constructor with method, URL, body and type but without headers.- 参数:
body
- the bodymethod
- the methodurl
- the URLtype
- the type used for generic type resolution- 从以下版本开始:
- 4.3
RequestEntity
public RequestEntity(MultiValueMap<String,String> headers, HttpMethod method, URI url)
Constructor with method, URL and headers but without body.- 参数:
headers
- the headersmethod
- the methodurl
- the URL
RequestEntity
public RequestEntity(T body, MultiValueMap<String,String> headers, HttpMethod method, URI url)
Constructor with method, URL, headers and body.- 参数:
body
- the bodyheaders
- the headersmethod
- the methodurl
- the URL
RequestEntity
public RequestEntity(T body, MultiValueMap<String,String> headers, HttpMethod method, URI url, Type type)
Constructor with method, URL, headers, body and type.- 参数:
body
- the bodyheaders
- the headersmethod
- the methodurl
- the URLtype
- the type used for generic type resolution- 从以下版本开始:
- 4.3
方法详细资料
getMethod
public HttpMethod getMethod()
Return the HTTP method of the request.- 返回:
- the HTTP method as an
HttpMethod
enum value
getType
public Type getType()
Return the type of the request's body.- 返回:
- the request's body type, or
null
if not known - 从以下版本开始:
- 4.3
equals
public boolean equals(Object other)
- 覆盖:
equals
在类中HttpEntity<T>
hashCode
public int hashCode()
- 覆盖:
hashCode
在类中HttpEntity<T>
toString
public String toString()
- 覆盖:
toString
在类中HttpEntity<T>
method
public static RequestEntity.BodyBuilder method(HttpMethod method, URI url)
Create a builder with the given method and url.- 参数:
method
- the HTTP method (GET, POST, etc)url
- the URL- 返回:
- the created builder
get
public static RequestEntity.HeadersBuilder<?> get(URI url)
Create an HTTP GET builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
head
public static RequestEntity.HeadersBuilder<?> head(URI url)
Create an HTTP HEAD builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
post
public static RequestEntity.BodyBuilder post(URI url)
Create an HTTP POST builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
put
public static RequestEntity.BodyBuilder put(URI url)
Create an HTTP PUT builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
patch
public static RequestEntity.BodyBuilder patch(URI url)
Create an HTTP PATCH builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
delete
public static RequestEntity.HeadersBuilder<?> delete(URI url)
Create an HTTP DELETE builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder
options
public static RequestEntity.HeadersBuilder<?> options(URI url)
Creates an HTTP OPTIONS builder with the given url.- 参数:
url
- the URL- 返回:
- the created builder