使用REST API时Spring RestTemplate连接被拒绝

时间:2017-11-15 07:01:32

标签: spring rest resttemplate

我正在使用RestTemplate来使用REST API。为了测试,我采用了Internet中的一个示例REST API。

`http://services.groupkt.com/country/get/iso2code/IN

这可以在浏览器中使用,并且能够看到带有200状态代码的响应。

{
"RestResponse" : {
"messages" : [ "Country found matching code [IN]." ],
"result" : {
  "name" : "India",
  "alpha2_code" : "IN",
  "alpha3_code" : "IND"
  }
 }
}

但是,当我尝试使用resttemplate时,它会给出连接拒绝异常。请找我的代码

RestTemplate restTemplate = new RestTemplate();         ResponseEntity response = restTemplate.getForEntity(“http://services.groupkt.com/country/get/iso2code/IN”,String.class);

`总异常详情

Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://services.groupkt.com/country/get/iso2code/IN": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
at com.bb.controller.TestController.main(TestController.java:44)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
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.web.client.RestTemplate.doExecute(RestTemplate.java:652)
... 3 more

0 个答案:

没有答案