I have to send GET request to some API without any parameters so I wrote the code:
public Boolean getData(String apiUrl) {
try {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiUrl, String.class);
if (responseEntity.getStatusCode().toString().equals("200")) {
theLogger.info("Server successfully answered with response code: {} - {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return true;
} else {
theLogger.error("Something goes wrong! Server answered with response code: {} and error: {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return false;
}
} catch (Exception theException) {
theException.printStackTrace();
theLogger.error("Cannot send channelInfo! Exception: " + theException);
return false;
}
}
It works when API url is HTTP, but doesn't work with HTTPS. It says:
sun.security.validator.ValidatorException: 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: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I believe it's because my code doesn't trust this certificate. But I don't know what should I do with it? The API is public and it opens in any web-browser it is just some https site. I couldn't believe that I should download certificates for all https sites which I work with and add this certificates somewhere. I believe that it should be the more common way to trust this public sites.