Spring RestTemplate客户端 - 连接被拒绝异常

13

我是webservices的新手,正在尝试使用RestTemplate编写RESTFul webservice的客户端。 我正在使用org.springframework.http.converter.xml.MarshallingHttpMessageConverter作为消息转换器和org.springframework.oxm.xstream.XStreamMarshaller作为marshaller。

是否有其他方法可以进一步调试或找出此问题的根本原因?

我的消费者类看起来像这样 -

@SuppressWarnings("unchecked")
public List<Deal> getClientInformation() throws RestClientException {
    return restTemplate.getForObject(webServiceURL, List.class);

}

异常:

Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error: Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:359)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:307)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:177)
at main.java.com.sample.consumer.DealConsumer.getClientInformation(Consumer.java:35)
at main.java.com.client.WebserviceConsumerTestClient.main(WebserviceConsumerTestClient.java:16)

Caused by: java.net.ConnectException: Connection refused: connect at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:359)


由于java.net.ConnectException:连接拒绝:连接失败,导致错误发生。在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:359)处发生。
1个回答

24

您尝试调用的 webServiceURL 无法访问。请确保 webServiceURL 路径正确且正在监听。

另外,还要检查服务器端是否存在防火墙问题。

可以使用 Wireshark 进一步进行调试。

http://www.wireshark.org/


4
谢谢,是防火墙问题。在客户端代码中调用web服务之前,通过添加以下两行代码解决了这个错误: System.setProperty("proxyHost", "yourproxy.server.com"); System.setProperty("proxyPort", "8080"); - Bruso
2
很好,现在如果问题已经解决,您可以将答案标记为正确的来关闭这个问题。 - dhamibirendra
@dhamibirendra 谢谢。 - UdayKiran Pulipati
@dhamibirendra 在出现“org.springframework.web.client.ResourceAccessException: I/O error on GET request for "<URL>": connect timed out; nested exception is java.net.SocketTimeoutException”这种间歇性错误时该怎么办? - Sam
@Sam,看起来你尝试连接的URL无法访问。你能ping通这个URL吗?如果这个URL无法访问,那么请联系你的网络管理员或负责将该URL暴露给你的系统的人员。 - dhamibirendra
@dhamibirendra 谢谢你们俩 - mtbadi39

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接