title: AWS API Gateway - Connection timeout on accessing API using Spring RestTemplate in Java where it works fine using Postman
body: <p>I have created an API on AWS API Gateway. When I access this using postman it works fine but when I am trying to access this in java using Spring RestTemplate, it gives me connection time out error. I tried with below different rest client approaches. But no luck. Not sure what mistake I am doing in code below. </p>
  
  <p>Please note, I changed URI and IPs in below code/exception as I don't want to disclose actual URl and IPs.</p>
  
  <p><strong>Approach-1</strong></p>
  
  <pre><code>final String uri = "https://xyz.execute-api.us-east-2.amazonaws.com/dev/dynamodb/users/55";
  
  RestTemplate restTemplate = new RestTemplate();
  String result = restTemplate.getForObject(uri, String.class);
  
  System.out.println(result);
  </code></pre>
  
  <p><strong>Approach-2</strong></p>
  
  <pre><code>CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
  
  HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
  requestFactory.setHttpClient(httpClient);
  
  RestTemplate restTemplate = new RestTemplate(requestFactory);
  
  final String uri = "https://xyz.execute-api.us-east-2.amazonaws.com/dev/dynamodb/users/55";
  
  String result = restTemplate.getForObject(uri, String.class); 
  
  System.out.println(result);  
  </code></pre>
  
  <p>Getting below exception when I call API using above java/spring code.</p>
  
  <pre><code>Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://xyz.execute-api.us-east-1.amazonaws.com/dev/dynamodb/users/55": Connect to xyz.execute-api.us-east-1.amazonaws.com:443 [xyz.execute-api.us-east-1.amazonaws.com/11.11.232.11, xyz.execute-api.us-east-1.amazonaws.com/52.200.98.93, xyz.execute-api.us-east-1.amazonaws.com/11.81.200.210] failed: Connection timed out: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to xyz.execute-api.us-east-1.amazonaws.com:443 [xyz.execute-api.us-east-1.amazonaws.com/11.11.232.11, xyz.execute-api.us-east-1.amazonaws.com/52.200.98.93, xyz.execute-api.us-east-1.amazonaws.com/11.81.200.210] failed: Connection timed out: connect
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
      at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:310)
      at com.adp.taxcredits.connector.icims.services.CachingServiceImpl.main(CachingServiceImpl.java:46)
  Caused by: org.apache.http.conn.HttpHostConnectException: Connect to xyz.execute-api.us-east-1.amazonaws.com:443 [xyz.execute-api.us-east-1.amazonaws.com/11.11.232.11, xyz.execute-api.us-east-1.amazonaws.com/52.200.98.93, xyz.execute-api.us-east-1.amazonaws.com/11.81.200.210] failed: Connection timed out: connect
      at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
      at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
      at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:394)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
      at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87)
      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:734)
      ... 3 more
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:339)
      at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
      ... 16 more
  </code></pre>