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

Spring Boot中GetMapping无法使用,访问localhost报404错误求助

解决Spring Boot项目访问404问题的排查步骤
  • 检查启动类的包路径
    Spring Boot默认仅扫描启动类所在包及其子包下的组件。你的控制器位于com.icttech.springBootDemo.apiDemo,请确认带@SpringBootApplication注解的启动类,其包路径是否为com.icttech.springBootDemo或更上层(如com.icttech)。若启动类在无关包下,需在启动类上添加@ComponentScan(basePackages = "com.icttech.springBootDemo")手动指定扫描范围。

  • 验证IntelliJ的运行配置
    确保使用Spring Boot类型的运行配置:

    1. 点击IntelliJ右上角运行配置下拉框,选择Edit Configurations
    2. 确认配置类型为Spring Boot,且主类已正确选择你的启动类
    3. 避免使用普通Java Application配置启动Spring Boot项目
  • 确认Web依赖是否正确引入
    检查pom.xml(Maven)或build.gradle(Gradle)中是否包含spring-boot-starter-web依赖:
    Maven示例:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

    缺少该依赖时,Spring Boot不会启动内嵌Tomcat容器,无法处理HTTP请求。

  • 查看启动日志中的路由映射
    启动项目后,在控制台日志中搜索是否存在类似以下内容:

    Mapped "{[/]}" onto public java.lang.String com.icttech.springBootDemo.apiDemo.ProductsController.get()
    Mapped "{[/products]}" onto public java.lang.String com.icttech.springBootDemo.apiDemo.ProductsController.get2()
    

    若没有这些日志,说明控制器未被Spring容器扫描注册,回到第一步检查包路径或扫描配置。

  • 排查是否有拦截类干扰
    检查项目中是否存在WebMvcConfigurer实现类或Spring Security配置,这类组件可能修改默认路由规则或拦截请求。可暂时注释相关配置,重启项目测试是否能正常访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 00:05:40