类 HttpComponentsClientHttpRequestFactory
- java.lang.Object
- org.springframework.http.client.HttpComponentsClientHttpRequestFactory
- 所有已实现的接口:
DisposableBean
,ClientHttpRequestFactory
public class HttpComponentsClientHttpRequestFactory extends Object implements ClientHttpRequestFactory, DisposableBean
ClientHttpRequestFactory
implementation that uses Apache HttpComponents HttpClient to create requests.Allows to use a pre-configured
HttpClient
instance - potentially with authentication, HTTP connection pooling, etc.NOTE: Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
- 从以下版本开始:
- 3.1
- 作者:
- Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll, Juergen Hoeller
构造器概要
构造器 构造器 说明 HttpComponentsClientHttpRequestFactory()
Create a new instance of theHttpComponentsClientHttpRequestFactory
with a defaultHttpClient
based on system properties.HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
Create a new instance of theHttpComponentsClientHttpRequestFactory
with the givenHttpClient
instance.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected org.apache.http.protocol.HttpContext
createHttpContext(HttpMethod httpMethod, URI uri)
Template methods that creates aHttpContext
for the given HTTP method and URI.protected HttpUriRequest
createHttpUriRequest(HttpMethod httpMethod, URI uri)
Create a Commons HttpMethodBase object for the given HTTP method and URI specification.ClientHttpRequest
createRequest(URI uri, HttpMethod httpMethod)
Create a newClientHttpRequest
for the specified URI and HTTP method.protected RequestConfig
createRequestConfig(Object client)
Create a defaultRequestConfig
to use with the given client.void
destroy()
Shutdown hook that closes the underlyingClientConnectionManager
's connection pool, if any.HttpClient
getHttpClient()
Return theHttpClient
used for synchronous execution.protected RequestConfig
mergeRequestConfig(RequestConfig clientConfig)
protected void
postProcessHttpRequest(HttpUriRequest request)
Template method that allows for manipulating theHttpUriRequest
before it is returned as part of aHttpComponentsClientHttpRequest
.void
setBufferRequestBody(boolean bufferRequestBody)
Indicates whether this request factory should buffer the request body internally.void
setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlyingRequestConfig
.void
setConnectTimeout(int timeout)
Set the connection timeout for the underlyingRequestConfig
.void
setHttpClient(HttpClient httpClient)
Set theHttpClient
used for synchronous execution.void
setHttpContextFactory(BiFunction<HttpMethod,URI,org.apache.http.protocol.HttpContext> httpContextFactory)
Configure a factory to pre-create theHttpContext
for each request.void
setReadTimeout(int timeout)
Set the socket read timeout for the underlyingRequestConfig
.
构造器详细资料
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory()
Create a new instance of theHttpComponentsClientHttpRequestFactory
with a defaultHttpClient
based on system properties.
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
Create a new instance of theHttpComponentsClientHttpRequestFactory
with the givenHttpClient
instance.- 参数:
httpClient
- the HttpClient instance to use for this request factory
方法详细资料
setHttpClient
public void setHttpClient(HttpClient httpClient)
Set theHttpClient
used for synchronous execution.
getHttpClient
public HttpClient getHttpClient()
Return theHttpClient
used for synchronous execution.
setConnectTimeout
public void setConnectTimeout(int timeout)
Set the connection timeout for the underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.This options does not affect connection timeouts for SSL handshakes or CONNECT requests; for that, it is required to use the
SocketConfig
on theHttpClient
itself.- 参数:
timeout
- the timeout value in milliseconds- 另请参阅:
RequestConfig.getConnectTimeout()
,SocketConfig.getSoTimeout()
setConnectionRequestTimeout
public void setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.- 参数:
connectionRequestTimeout
- the timeout value to request a connection in milliseconds- 另请参阅:
RequestConfig.getConnectionRequestTimeout()
setReadTimeout
public void setReadTimeout(int timeout)
Set the socket read timeout for the underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.- 参数:
timeout
- the timeout value in milliseconds- 另请参阅:
RequestConfig.getSocketTimeout()
setBufferRequestBody
public void setBufferRequestBody(boolean bufferRequestBody)
Indicates whether this request factory should buffer the request body internally.Default is
true
. When sending large amounts of data via POST or PUT, it is recommended to change this property tofalse
, so as not to run out of memory.- 从以下版本开始:
- 4.0
setHttpContextFactory
public void setHttpContextFactory(BiFunction<HttpMethod,URI,org.apache.http.protocol.HttpContext> httpContextFactory)
Configure a factory to pre-create theHttpContext
for each request.This may be useful for example in mutual TLS authentication where a different
RestTemplate
for each client certificate such that all calls made through a givenRestTemplate
instance as associated for the same client identity.HttpClientContext.setUserToken(Object)
can be used to specify a fixed user token for all requests.- 参数:
httpContextFactory
- the context factory to use- 从以下版本开始:
- 5.2.7
createRequest
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException
从接口复制的说明:ClientHttpRequestFactory
Create a newClientHttpRequest
for the specified URI and HTTP method.The returned request can be written to, and then executed by calling
ClientHttpRequest.execute()
.- 指定者:
createRequest
在接口中ClientHttpRequestFactory
- 参数:
uri
- the URI to create a request forhttpMethod
- the HTTP method to execute- 返回:
- the created request
- 抛出:
IOException
- in case of I/O errors
createRequestConfig
@Nullable protected RequestConfig createRequestConfig(Object client)
Create a defaultRequestConfig
to use with the given client. Can returnnull
to indicate that no custom request config should be set and the defaults of theHttpClient
should be used.The default implementation tries to merge the defaults of the client with the local customizations of this factory instance, if any.
- 参数:
client
- theHttpClient
(orHttpAsyncClient
) to check- 返回:
- the actual RequestConfig to use (may be
null
) - 从以下版本开始:
- 4.2
- 另请参阅:
mergeRequestConfig(RequestConfig)
mergeRequestConfig
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig)
- 参数:
clientConfig
- the config held by the current- 返回:
- the merged request config
- 从以下版本开始:
- 4.2
createHttpUriRequest
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri)
Create a Commons HttpMethodBase object for the given HTTP method and URI specification.- 参数:
httpMethod
- the HTTP methoduri
- the URI- 返回:
- the Commons HttpMethodBase object
postProcessHttpRequest
protected void postProcessHttpRequest(HttpUriRequest request)
Template method that allows for manipulating theHttpUriRequest
before it is returned as part of aHttpComponentsClientHttpRequest
.The default implementation is empty.
- 参数:
request
- the request to process
createHttpContext
@Nullable protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, URI uri)
Template methods that creates aHttpContext
for the given HTTP method and URI.The default implementation returns
null
.- 参数:
httpMethod
- the HTTP methoduri
- the URI- 返回:
- the http context
destroy
public void destroy() throws Exception
Shutdown hook that closes the underlyingClientConnectionManager
's connection pool, if any.- 指定者:
destroy
在接口中DisposableBean
- 抛出:
Exception
- in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.