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

问题描述 投票:0回答:2

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

有什么方法可以进一步调试或找出此问题的根本原因吗?

我的消费者阶层是这样的 -

@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)

原因:java.net.ConnectException:连接被拒绝:连接 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:359)

web-services spring rest client
2个回答
24
投票

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

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

Wireshark 可以帮助您进一步调试。

http://www.wireshark.org/


0
投票

服务器未运行:确保在 https://localhost:1112 上运行的服务器已启动并正在运行。您可以尝试直接从浏览器访问该 URL 或使用curl 等工具来查看服务器是否响应。

正确的 URL:仔细检查 URL 端点 (/erm-app/fetchOfcByLevel) 并确保其正确。小拼写错误或不正确的端点可能会导致连接错误。

SSL/TLS 配置:由于您使用的是 HTTPS,请确保服务器端的 SSL/TLS 配置正确设置。如果 SSL/TLS 握手出现问题,连接可能会被拒绝。

防火墙或网络限制:有时,网络防火墙或其他安全配置可能会阻止与特定端口的连接。确保不存在可能阻止您的连接的此类限制。

本地开发:如果您在同一台计算机上运行客户端和服务器进行本地开发,请确保服务器实际上在端口 1112 上运行并且设置为接受连接。

日志和调试:检查服务器应用程序的日志(https://localhost:1112)。通常,服务器日志可以提供有关连接被拒绝的原因或服务器端是否存在任何问题的见解。

外部依赖项:如果您的 Spring Boot 应用程序依赖于其他服务,请确保这些服务也正在运行且可访问。

© www.soinside.com 2019 - 2024. All rights reserved.