When using Apache HttpClient with Spring Boot’s RestTemplate, you can configure the RestTemplate to use the HttpClient as the underlying HTTP client. Here’s an example of how to configure RestTemplate with Apache HttpClient in a Spring Boot application:

  • Add the necessary dependencies to your project. Include both the spring-boot-starter-web and httpclient dependencies in your pom.xml file:
    <!-- Spring Boot Web Starter -->

    <!-- Apache HttpClient -->
  • Create a configuration class to configure the RestTemplate with Apache HttpClient. Here’s an example:
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class RestTemplateConfig {

    public RestTemplate restTemplate() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

        CloseableHttpClient httpClient = HttpClientBuilder.create()

        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        return new RestTemplate(requestFactory);


In this configuration class, we create an instance of HttpClient using HttpClientBuilder. Then, we create an instance of HttpComponentsClientHttpRequestFactory with the HttpClient instance.

Finally, we create a RestTemplate instance and set the HttpComponentsClientHttpRequestFactory as the request factory for the RestTemplate.

  • Use the configured RestTemplate in your application. Inject the RestTemplate bean where you need to make HTTP requests. Here’s an example of using the RestTemplate in a service class:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

public class MyService {

    private final RestTemplate restTemplate;

    public MyService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

    public void performRequest() {
        // Use the RestTemplate to make HTTP requests
        String response = restTemplate.getForObject("https://api.example.com/data", String.class);
        System.out.println("Response: " + response);


In this example, the RestTemplate is injected into the MyService class using constructor injection. You can then use the RestTemplate to make HTTP requests.

By default, the RestTemplate will use Apache HttpClient as the underlying HTTP client due to the configuration provided.

With this configuration, you can use the RestTemplate in your Spring Boot application and benefit from the features and capabilities of Apache HttpClient.