You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TestRestTemplate的getForEntity方法执行GET请求时触发I/O错误求助

排查TestRestTemplate调用出现Connection refused异常的方案

问题代码

var restTemplate = new TestRestTemplate();
var actual = restTemplate.getForEntity(new URI("http://localhost:8080/foo"), FooDto.class);

抛出异常

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/foo": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

排查步骤

  • 确认目标服务状态:直接通过浏览器或curl http://localhost:8080/foo访问接口,验证服务是否正常启动并监听8080端口。
  • 核对服务端口配置:检查目标服务的配置文件(如application.properties),确认server.port设置为8080,避免端口配置不一致。
  • 修正网络访问地址:若测试代码和目标服务不在同一环境(如容器、虚拟机),localhost可能指向测试环境而非服务所在环境,需替换为服务实际的IP或主机名。
  • 检查防火墙与代理:本地防火墙可能拦截8080端口请求,或测试环境配置了代理导致连接失败,需关闭防火墙或调整代理设置。
  • 验证TestRestTemplate配置:若自定义了TestRestTemplate的客户端参数(如超时、代理),检查配置是否正确,避免因配置错误阻断连接。
  • 确认接口路径有效性:虽然连接拒绝通常是服务未启动,但仍需核对/foo接口是否存在于目标服务中,排除路径错误引发的间接问题。

内容的提问来源于stack exchange,提问作者Kaytus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 17:10:15