I have a code given below:
RestTemplate restTemplate = new RestTemplate();
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient closableHttpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient((HttpClient) closableHttpClient);
restTemplate = new RestTemplate(requestFactory);
In the above code, I am trying to skip the SSL validation.
Issue: When upgrading from Spring Boot version 2.7 to 3.3, according to docs for migrating from Apache httpclient4 to httpclient5, it’s mentioned that HttpComponentsClientHttpRequestFactory
got deprecated.
How to resolve this issue?
Vasa kirankumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
HttpComponentsClientHttpRequestFactory
is not deprecated in Spring Boot 3.3.
Read the Java Docs here.
I have updated your code to fix the compatibility issue with httpclient5.
RestTemplate restTemplate = new RestTemplate();
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
HttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(csf).build();
CloseableHttpClient closableHttpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(closableHttpClient);
restTemplate = new RestTemplate(requestFactory);
See if this helps.