I am trying to call an external API that has HTTPS in it. When I invoke it from my Spring Boot Application using Rest Template I get the following error:
I/O error on POST request for "https://url-path": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I generated a self-signed certificate using keytool
. I tried the following command:
sudo keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass <my-password> -validity 360 -keysize 2048
I was able to generate a file keystore.jks
which I have placed in my spring boot project at the root.
My code to invoke the external API is:
CloseableHttpClient httpClient
= HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<String> requestEntity = new HttpEntity<String>(getHeaders());
ResponseEntity<Object> responseEntity = restTemplate.exchange(URL_TO_CALL,
HttpMethod.POST,
null, Object.class);
Object result = responseEntity.getBody();
My application.properties
file has the following:
server.ssl.key-store=keystore.jks
server.ssl.key-store-password=<my-password-given-while-generating-certificate>
server.ssl.trust-store-provider=SUN
I am unable to figure out where I am going wrong.