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

调用RESTful Web Service遇阻:Java Swing环境下404错误求助

问题排查方案

从你给出的报错信息(连接8081失败+404 NotFound)来看,问题集中在服务可用性和路径匹配上,按以下步骤逐一排查:

1. 确认REST服务端的状态与端口

  • 打开服务端的application.properties,检查server.port配置值是否为8081。如果不是,同步修改客户端调用的端口。
  • 查看服务端启动日志,确认是否出现类似Started XxxApplication in X seconds的成功启动提示。若服务端启动失败,优先解决启动问题(比如依赖缺失、配置错误)。
  • 在CMD中执行curl http://localhost:8081或telnet localhost 8081,测试端口是否能连通。若不通,检查端口是否被其他程序占用,或本地防火墙是否拦截了8081端口。

2. 核对请求路径与服务端映射

  • 检查服务端Controller类,确认映射路径完全匹配/api/random:
    @RestController
    public class RandomController {
        @GetMapping("/api/random") // 路径需完全一致,包括斜杠、大小写
        public SomeObject getRandom() {
            // 业务逻辑
        }
    }
    
  • 确认Controller被Spring扫描到:Controller所在包需与Spring Boot主类(带@SpringBootApplication的类)在同一根包下,或主类通过@ComponentScan指定扫描范围。
  • 检查服务端是否配置了全局路径前缀(比如server.servlet.context-path=/app),若有,请求路径需改为/app/api/random。

3. 检查客户端调用配置

  • 确认客户端代码中的请求URL无误:
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8081/api/random"; // 端口、路径需与服务端完全一致
    SomeObject result = restTemplate.getForObject(url, SomeObject.class);
    
  • 若使用Swing界面,检查触发请求的代码是否存在拼写错误(比如把8081写成8080,或路径少写了/api)。

4. 排查环境与依赖问题

  • 在Eclipse中右键项目→Run As→Maven Clean,再执行Run As→Maven Install,确保依赖正确下载、项目无编译错误。
  • 确认服务端pom.xml包含spring-boot-starter-web依赖,客户端若为Spring项目,需确保REST相关依赖正确引入。

附你提供的报错片段:

2023-11-20T18:22:41.291+08:00 ERROR 6844 --- [           main] o.s.boot.SpringApplication              : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner

...

Caused by: org.springframework.web.client.HttpClientErrorException$NotFound: 404 : "{\"timestamp\":\"2023-11-20T10:22:41.242+00:00\",\"status\":404,\"error\":\"Not Found\",\"path\":\"/api/random\"}"

内容的提问来源于stack exchange,提问作者YI Chen Wang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 21:58:13