How to call HTTPS restful web services using Spring RestTemplate How to call HTTPS restful web services using Spring RestTemplate spring spring

How to call HTTPS restful web services using Spring RestTemplate


Either you need to have certificates in your keystore or you can accept all certificates (kind off ignore certificate validation)

So you can re-define bean of rest template as

import javax.net.ssl.SSLContext;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.TrustStrategy;import java.security.cert.X509Certificate;@Beanpublic RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {    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 httpClient = HttpClients.custom()                    .setSSLSocketFactory(csf)                    .build();    HttpComponentsClientHttpRequestFactory requestFactory =                    new HttpComponentsClientHttpRequestFactory();    requestFactory.setHttpClient(httpClient);    RestTemplate restTemplate = new RestTemplate(requestFactory);    return restTemplate; }

You should not need additional jars except for apache core, client and dependencies.