Pranav MS
Pranav MS

Reputation: 2296

I/O error on POST request for in SPRINGBOOT

I have googled it with issue. But those are not able to solve my problem.I have an issue with the SSL certificate in my code while calling a rest control.

My control method is:

@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/token", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody TokenModel GetToken(@RequestBody RequestBodyJson requestBodyJson) {
    TokenModel response = null; 
    RestTemplate restTemplate = new RestTemplate();
    try {                          
        response = new GenericRestClient<RequestBodyJson, TokenModel>().execute(
            new RequestDetails(url, HttpMethod.POST), requestBodyJson, responseHandler,TokenModel.class);
        restTemplate.getForEntity(url , TokenModel.class);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return response;
}

Error is:-I/O error on POST request for "https://myurl.com": 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

can anyone can help me here?

Upvotes: 2

Views: 55624

Answers (2)

MHK
MHK

Reputation: 489

If your environment uses a proxy/VPN so you have to configure proxy even your using RestTemplate or RestClient.

put this piece of code in where your initiating RestTemplate or RestClient instance.

System.setProperty("http.proxyHost","YouProxyHost"); System.setProperty("http.proxyport","YouProxyPort"); System.setProperty("https.proxyHost","YouProxyHost"); System.setProperty("https.proxyPort","YouProxyPort");

Upvotes: 1

Pranav MS
Pranav MS

Reputation: 2296

Thanks guys for your help.

I have solved the problem by

In my POM.XML i have added one dependency

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5</version>
</dependency>

Then my JAVA code is

@RequestMapping(value = "/gettokens", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody ResponseEntity<TokenModel> GetTokens(@RequestBody RequestBodyJson requestBodyJson)
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    ResponseEntity<TokenModel> response = null;
    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);       
    try {                          
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        Map map = new HashMap<String, String>();
        map.put("Content-Type", "application/json");
        headers.setAll(map);
        HttpEntity<?> _HttpEntityRequestBodyJson = new HttpEntity<>(requestBodyJson, headers); 
        response= restTemplate.exchange(url, HttpMethod.POST,_HttpEntityRequestBodyJson, new ParameterizedTypeReference<TokenModel>() {});  
        System.out.println(response.getBody());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return response;
}

Upvotes: 2

Related Questions