close

[Solved] I/O error on POST request for in SPRINGBOOT

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error I/O error on POST request for in SPRINGBOOT in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How I/O error on POST request for in SPRINGBOOT Error Occurs?

Today I am facing the following error I/O error on POST request for in SPRINGBOOT in Java.

How To Solve I/O error on POST request for in SPRINGBOOT Error ?

  1. How To Solve I/O error on POST request for in SPRINGBOOT Error ?

    To Solve I/O error on POST request for in SPRINGBOOT Error 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>

  2. I/O error on POST request for in SPRINGBOOT

    To Solve I/O error on POST request for in SPRINGBOOT Error 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>

Solution 1

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;
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read