In Spring Framework 6, support for Apache HttpClient 4.x has been removed from HttpComponentsClientHttpRequestFactory. Components that rely on this factory, such as RestTemplate must now use Apache HttpClient 5.x, which uses the org.apache.hc.* package.
Any usage of HttpClient 4.x classes such as CloseableHttpClient, HttpClient, RequestConfig, or HttpContext from the org.apache.http.* package within Spring’s HTTP client infrastructure (including constructors, method invocations, or configurations involving HttpComponentsClientHttpRequestFactory) can lead to compilation errors and must be updated to use the HttpClient 5.x equivalents.
Review the following methods and constructors from HttpComponentsClientHttpRequestFactory to ensure they do not reference HttpClient 4.x APIs:
HttpComponentsClientHttpRequestFactory(HttpClient)setHttpClient(HttpClient)getHttpClient()mergeRequestConfig(RequestConfig)postProcessHttpRequest(HttpUriRequest)createHttpUriRequest(HttpMethod, URI)createHttpContext(HttpMethod, URI)createRequestConfig(Object)setHttpContextFactory(BiFunction)The following example shows invalid usage that will be flagged:
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class InvalidHttp4ClientUsage {
public void example() {
CloseableHttpClient client = HttpClients.custom().build();
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}
}
The following example shows valid migrated usage:
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class ValidHttpClientUsage {
public void example() {
CloseableHttpClient client = HttpClients.custom().build();
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}
}
For more information, see the following resources: