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

Spring Boot+Docker+Liquibase环境下/userList接口404问题排查

修复Spring Boot接口404问题的步骤

1. 检查组件扫描范围

Spring Boot默认仅扫描主应用类所在包及其子包下的组件。假设你的主应用类CarserviceApplication位于ru.ithub.carservice包下,若Controller类在ru.ithub或其他非子包路径中,Spring将无法自动识别并注册该控制器。

解决方式:

  • 将Controller类移动到主应用类的子包中,比如ru.ithub.carservice.controller;
  • 或者在主应用类上添加@ComponentScan注解,指定扫描范围:
@SpringBootApplication
@ComponentScan(basePackages = {"ru.ithub.controller", "ru.ithub.carservice"})
public class CarserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(CarserviceApplication.class, args);
    }
}

2. 检查上下文路径配置

查看application.yml是否配置了server.servlet.context-path,如果存在该配置,访问接口时需要加上对应前缀。例如配置了:

server:
  servlet:
    context-path: /api

则正确访问路径应为/api/userList,而非直接/userList。

3. 验证Controller是否完成注册

启动应用后,查看控制台日志,寻找类似以下的映射记录:

Mapped "{[/userList],methods=[GET]}" onto public java.util.List<ru.ithub.model.User> ru.ithub.controller.Controller.getAllUsers()

如果无此日志,说明Controller未被Spring识别,需回到第一步重新检查组件扫描配置。

4. 排查应用启动异常

确认应用启动过程中无报错,若数据库连接失败、Liquibase执行出错等,可能导致控制器组件未完成初始化。优先解决启动日志中的错误后,再测试接口。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:23:12