Skip to content

BAEL-9334: Resolving Apache HttpClient Error When Migrating Spring Bo… #18645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-modules/spring-boot-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.restclient;

import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.util.Timeout;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfiguration {

@Bean
public RestTemplate restTemplate() {
try {
// Timeout configurations
SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(Timeout.ofSeconds(30)) // Read timeout
.build();

ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(30)) // Connect timeout
.build();

RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(Timeout.ofSeconds(30)) // Pool wait timeout
.build();

// Connection pool configuration
PoolingHttpClientConnectionManager connectionManager =
PoolingHttpClientConnectionManagerBuilder.create()
.setMaxConnPerRoute(20)
.setMaxConnTotal(100)
.setDefaultSocketConfig(socketConfig)
.setDefaultConnectionConfig(connectionConfig)
.build();

CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.build();

return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));

} catch (Exception e) {
throw new IllegalStateException("Failed to configure RestTemplate", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.baeldung.restclient;

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@ContextConfiguration(classes = RestTemplateConfiguration.class)
public class RestTemplateConfigurationUnitTest {

@Autowired
private ApplicationContext context;

@Test
public void givenSpringContext_whenRestTemplateBeanRetrieved_thenReturnsProperlyConfiguredInstance() {
// When - We retrieve the RestTemplate bean
RestTemplate restTemplate = context.getBean(RestTemplate.class);

// Then - Verify it's properly configured
assertThat(restTemplate)
.isNotNull()
.extracting(RestTemplate::getRequestFactory)
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);

HttpComponentsClientHttpRequestFactory factory =
(HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();

assertThat(factory.getHttpClient())
.isInstanceOfSatisfying(CloseableHttpClient.class, client -> {
assertThat(client).isNotNull();
});
}
}