I am trying to migration some codes from Spring Boot 2 to 3.1, and in the process it seems HttpClient needs to be upgraded from 4 to client5 as well.
The following code is facing an error during Gradle build,
// import org.apache.http.client.HttpClient;
import org.apache.hc.client5.http.classic.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class RestTemplateHttpClientCustomizer implements RestTemplateCustomizer {
@Autowired HttpClient httpClient;
@Override
public void customize(RestTemplate restTemplate) {
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate.setRequestFactory(requestFactory);
}
}
The error is:
Field httpClient in
com.xxx.commons.web.client.config.RestTemplateHttpClientCustomizer
required a bean of type
‘org.apache.hc.client5.http.classic.HttpClient’ that could not be
found.The injection point has the following annotations:
– @org.springframework.beans.factory.annotation.Autowired(required=true)Action: Consider defining a bean of type
‘org.apache.hc.client5.http.classic.HttpClient’ in your configuration.
Anyone know how may I modify the codes above to add the client5 HttpClient bean?
Our target build environments is Java 21, Gradle 8.7, and Spring Boot 3.1.12