title: Spring RestTemplate HTTPs service call org.springframework.web.client.ResourceAccessException: I/O error on POST request
body: <p>I have to test Rest service. When I am trying with postman or curl utility it is working fine. But when I execute it using Spring RestTemplate I am getting below error. Any idea what's wrong? I am using spring boot 1,5, Spring 4, SPring STS</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://uat.pci.api.testconsumer.com/digital/dpu/charges/bankverification/v1": Software caused connection abort: recv failed; nested exception is java.net.SocketException: Software caused connection abort: recv failed
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620)
      at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:414)
      at com.testconsumer.consumer.workflow.integration.service.BankVerificationService.verifyBankDetails(BankVerificationService.java:113)
      at com.testconsumer.consumer.workflow.integration.bankverification.BankVerificationTest.testBankVerification(BankVerificationTest.java:43)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
  Caused by: java.net.SocketException: Software caused connection abort: recv failed
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(SocketInputStream.java:152)
      at java.net.SocketInputStream.read(SocketInputStream.java:122)
      at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
      at sun.security.ssl.InputRecord.read(InputRecord.java:480)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
      at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)
      at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
      at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
      at org.springframework.http.client.BufferingClientHttpRequestWrapper.executeInternal(BufferingClientHttpRequestWrapper.java:56)
      at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
      at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
      at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:99)
      at com.testconsumer.consumer.workflow.integration.config.RestResponseLogger.intercept(RestResponseLogger.java:26)
      at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:86)
      at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:70)
      at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
      at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:659)
      ... 33 more
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>@Bean
  public SimpleClientHttpRequestFactory clientHttpRequestFactory() {
      SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
      clientHttpRequestFactory.setConnectTimeout(connectiontimeout);
      clientHttpRequestFactory.setReadTimeout(readtimeout);
  
      return clientHttpRequestFactory;
  }
  
  @Bean(name = "bankSerivcerestTemplate")
  RestTemplate bankSerivcerestTemplate(SimpleClientHttpRequestFactory clientHttpRequestFactory) {
  
      ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(clientHttpRequestFactory);
  
      RestTemplate restTemplate = new RestTemplate(factory);
  
      restTemplate.getInterceptors().add(new RestResponseLogger());
  
      restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
          @Override
          public boolean hasError(ClientHttpResponse response) throws IOException {
              try {
                  return super.hasError(response);
              } catch (Exception e) {
                  logger.error("Exception [" + e.getMessage() + "] occurred while trying to send the request", e);
                  return true;
              }
          }
  
          @Override
          public void handleError(ClientHttpResponse response) throws IOException {
              try {
                  super.handleError(response);
              } catch (Exception e) {
                  logger.error("Exception [" + e.getMessage() + "] occurred while trying to send the request", e);
                  throw e;
              }
          }
      });
  
      return restTemplate;
  }
  
  public ResponseEntity&lt;ObjectNode&gt; verifyBankDetails(RestRequest request){
  
      MultiValueMap&lt;String, String&gt; headers = new LinkedMultiValueMap&lt;&gt;();
      //Headers
  
      Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
      //Params
  
      HttpEntity&lt;Object&gt; httpEntity = new HttpEntity&lt;Object&gt;(params, headers);
  
      ResponseEntity&lt;ObjectNode&gt; response = restTemplate.postForEntity(url, httpEntity, ObjectNode.class);
      JsonNode root = objectMapper.valueToTree(response.getBody());
  
      //Parse response
  
      return response;
  }
  </code></pre>