Top Description Fields Constructors Methods
org.springframework.http.client

public Class HttpComponentsClientHttpRequestFactory

extends Object
implements ClientHttpRequestFactory, DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean, org.springframework.http.client.ClientHttpRequestFactory
Imports
java.io.Closeable, .IOException, java.net.URI, java.time.Duration, java.util.concurrent.TimeUnit, java.util.function.BiFunction, org.apache.hc.client5.http.classic.HttpClient, org.apache.hc.client5.http.classic.methods.HttpDelete, .HttpGet, .HttpHead, .HttpOptions, .HttpPatch, .HttpPost, .HttpPut, .HttpTrace, org.apache.hc.client5.http.config.Configurable, .RequestConfig, org.apache.hc.client5.http.impl.classic.HttpClients, org.apache.hc.client5.http.io.HttpClientConnectionManager, org.apache.hc.client5.http.protocol.HttpClientContext, org.apache.hc.core5.http.ClassicHttpRequest, org.apache.hc.core5.http.io.SocketConfig, org.apache.hc.core5.http.protocol.HttpContext, org.springframework.beans.factory.DisposableBean, org.springframework.http.HttpMethod, org.springframework.lang.Nullable, org.springframework.util.Assert

org.springframework.http.client.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 5.1 or higher, as of Spring 6.0.

Authors
Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll, Juergen Hoeller
Since
3.1

Field Summary

Modifier and TypeField and Description
private long
private long
private HttpClient
private BiFunction<HttpMethod, URI, HttpContext>

Constructor Summary

AccessConstructor and Description
public
HttpComponentsClientHttpRequestFactory()

Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient based on system properties.

public
HttpComponentsClientHttpRequestFactory(HttpClient
the HttpClient instance to use for this request factory
httpClient
)

Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.

Method Summary

Modifier and TypeMethod and Description
protected HttpContext

Returns:

the http context
createHttpContext
(HttpMethod
the HTTP method
httpMethod
,
URI
the URI
uri
)

Template methods that creates a HttpContext for the given HTTP method and URI.

protected ClassicHttpRequest

Returns:

the Commons HttpMethodBase object
createHttpUriRequest
(HttpMethod
the HTTP method
httpMethod
,
URI
the URI
uri
)

Create a Commons HttpMethodBase object for the given HTTP method and URI specification.

public ClientHttpRequest
createRequest(URI
the URI to create a request for
uri
,
HttpMethod
the HTTP method to execute
httpMethod
)

Implements org.springframework.http.client.ClientHttpRequestFactory.createRequest.

Create a new ClientHttpRequest for the specified URI and HTTP method.

protected RequestConfig

Returns:

the actual RequestConfig to use (may be null)
createRequestConfig
(Object
the HttpClient (or HttpAsyncClient) to check
client
)

Create a default RequestConfig to use with the given client.

public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Shutdown hook that closes the underlying ClientConnectionManager's connection pool, if any.

public HttpClient
getHttpClient()

Return the HttpClient used for synchronous execution.

protected RequestConfig

Returns:

the merged request config
mergeRequestConfig
(RequestConfig
the config held by the current
clientConfig
)

Merge the given HttpClient-level RequestConfig with the factory-level configuration, if necessary.

protected void
postProcessHttpRequest(ClassicHttpRequest
the request to process
request
)

Template method that allows for manipulating the ClassicHttpRequest before it is returned as part of a HttpComponentsClientHttpRequest.

public void
setBufferRequestBody(boolean bufferRequestBody)
Deprecated for removal since 6.1. since 6.1 requests are never buffered, as if this property is false

Indicates whether this request factory should buffer the request body internally.

public void
setConnectionRequestTimeout(int
the timeout value to request a connection in milliseconds
connectionRequestTimeout
)

Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig.

public void
setConnectionRequestTimeout(Duration
the timeout value to request a connection in milliseconds
connectionRequestTimeout
)

Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig.

public void
setConnectTimeout(int
the timeout value in milliseconds
connectTimeout
)

Set the connection timeout for the underlying RequestConfig.

public void
setConnectTimeout(Duration
the timeout value in milliseconds
connectTimeout
)

Set the connection timeout for the underlying RequestConfig.

public void
setHttpClient(HttpClient httpClient)

Set the HttpClient used for synchronous execution.

public void
setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext>
the context factory to use
httpContextFactory
)

Configure a factory to pre-create the HttpContext for each request.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionRequestTimeoutback to summary
private long connectionRequestTimeout
connectTimeoutback to summary
private long connectTimeout
httpClientback to summary
private HttpClient httpClient
httpContextFactoryback to summary
private BiFunction<HttpMethod, URI, HttpContext> httpContextFactory
Annotations
@Nullable

Constructor Detail

HttpComponentsClientHttpRequestFactoryback to summary
public HttpComponentsClientHttpRequestFactory()

Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient based on system properties.

HttpComponentsClientHttpRequestFactoryback to summary
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)

Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.

Parameters
httpClient:HttpClient

the HttpClient instance to use for this request factory

Method Detail

createHttpContextback to summary
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri)

Template methods that creates a HttpContext for the given HTTP method and URI.

The default implementation returns null.

Parameters
httpMethod:HttpMethod

the HTTP method

uri:URI

the URI

Returns:HttpContext

the http context

Annotations
@Nullable
createHttpUriRequestback to summary
protected ClassicHttpRequest createHttpUriRequest(HttpMethod httpMethod, URI uri)

Create a Commons HttpMethodBase object for the given HTTP method and URI specification.

Parameters
httpMethod:HttpMethod

the HTTP method

uri:URI

the URI

Returns:ClassicHttpRequest

the Commons HttpMethodBase object

createRequestback to summary
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException

Implements org.springframework.http.client.ClientHttpRequestFactory.createRequest.

Doc from org.springframework.http.client.ClientHttpRequestFactory.createRequest.

Create a new ClientHttpRequest for the specified URI and HTTP method.

The returned request can be written to, and then executed by calling ClientHttpRequest#execute().

Parameters
uri:URI

the URI to create a request for

httpMethod:HttpMethod

the HTTP method to execute

Returns:ClientHttpRequest

the created request

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
createRequestConfigback to summary
protected RequestConfig createRequestConfig(Object client)

Create a default RequestConfig to use with the given client. Can return null to indicate that no custom request config should be set and the defaults of the HttpClient should be used.

The default implementation tries to merge the defaults of the client with the local customizations of this factory instance, if any.

Parameters
client:Object

the HttpClient (or HttpAsyncClient) to check

Returns:RequestConfig

the actual RequestConfig to use (may be null)

Annotations
@Nullable
Since
4.2
See Also
mergeRequestConfig(RequestConfig)
destroyback to summary
public void destroy() throws Exception

Implements org.springframework.beans.factory.DisposableBean.destroy.

Shutdown hook that closes the underlying ClientConnectionManager's connection pool, if any.

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.beans.factory.DisposableBean.destroy.

in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.

getHttpClientback to summary
public HttpClient getHttpClient()

Return the HttpClient used for synchronous execution.

mergeRequestConfigback to summary
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig)

Merge the given HttpClient-level RequestConfig with the factory-level configuration, if necessary.

Parameters
clientConfig:RequestConfig

the config held by the current

Returns:RequestConfig

the merged request config

Annotations
@SuppressWarnings:deprecation
Since
4.2
postProcessHttpRequestback to summary
protected void postProcessHttpRequest(ClassicHttpRequest request)

Template method that allows for manipulating the ClassicHttpRequest before it is returned as part of a HttpComponentsClientHttpRequest.

The default implementation is empty.

Parameters
request:ClassicHttpRequest

the request to process

setBufferRequestBodyback to summary
public void setBufferRequestBody(boolean bufferRequestBody)

Deprecated

for removal since 6.1.

since 6.1 requests are never buffered, as if this property is false

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 to false, so as not to run out of memory.

Annotations
@Deprecated
since:6.1
forRemoval:true
Since
4.0
setConnectionRequestTimeoutback to summary
public void setConnectionRequestTimeout(int connectionRequestTimeout)

Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

Parameters
connectionRequestTimeout:int

the timeout value to request a connection in milliseconds

See Also
RequestConfig#getConnectionRequestTimeout()
setConnectionRequestTimeoutback to summary
public void setConnectionRequestTimeout(Duration connectionRequestTimeout)

Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

Parameters
connectionRequestTimeout:Duration

the timeout value to request a connection in milliseconds

Since
6.1
See Also
RequestConfig#getConnectionRequestTimeout()
setConnectTimeoutback to summary
public void setConnectTimeout(int connectTimeout)

Set the connection timeout for the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

This options does not affect connection timeouts for SSL handshakes or CONNECT requests; for that, it is required to use the SocketConfig on the HttpClient itself.

Parameters
connectTimeout:int

the timeout value in milliseconds

See Also
RequestConfig#getConnectTimeout(), SocketConfig#getSoTimeout
setConnectTimeoutback to summary
public void setConnectTimeout(Duration connectTimeout)

Set the connection timeout for the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

This options does not affect connection timeouts for SSL handshakes or CONNECT requests; for that, it is required to use the SocketConfig on the HttpClient itself.

Parameters
connectTimeout:Duration

the timeout value in milliseconds

Since
6.1
See Also
RequestConfig#getConnectTimeout(), SocketConfig#getSoTimeout
setHttpClientback to summary
public void setHttpClient(HttpClient httpClient)

Set the HttpClient used for synchronous execution.

setHttpContextFactoryback to summary
public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpContextFactory)

Configure a factory to pre-create the HttpContext 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 given RestTemplate instance as associated for the same client identity. HttpClientContext#setUserToken(Object) can be used to specify a fixed user token for all requests.

Parameters
httpContextFactory:BiFunction<HttpMethod, URI, HttpContext>

the context factory to use

Since
5.2.7