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

Spring Boot 404错误求助:控制器路径位置引发的异常问题

排查Spring Boot 404白标错误的思路

下午好!刚上手Spring Boot碰到这种问题太常见了,我来帮你捋捋可能的原因~

你提到只有把EventoController.java放在br.com.SpringApp.SpringApp路径下才能正常运行,核心问题大概率出在Spring Boot的组件扫描规则上,下面是具体的排查步骤:

  • 确认启动类的位置
    Spring Boot默认会从启动类(带@SpringBootApplication注解的类)所在的包及其所有子包扫描@Controller、@Service、@Repository这些注解的类。如果你的启动类是在br.com.SpringApp.SpringApp下,那只有这个包及其子包里的Controller会被识别,其他包下的Controller会被忽略,自然就返回404了。

  • 手动指定组件扫描范围
    如果不想调整Controller的位置,可以在启动类的@SpringBootApplication注解里手动指定要扫描的包,比如你想扫描整个br.com.SpringApp下的所有类,就这么写:

    @SpringBootApplication(scanBasePackages = "br.com.SpringApp")
    public class SpringAppApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringAppApplication.class, args);
        }
    }
    
  • 检查Controller的注解和路径
    先确认你的EventoController上有没有正确标注@Controller或者@RestController注解,同时@RequestMapping的路径也别写错。举个正确的例子:

    @RestController
    @RequestMapping("/eventos")
    public class EventoController {
        @GetMapping("/listar")
        public String getEventos() {
            return "Lista de eventos";
        }
    }
    

    访问的时候要对应路径,比如http://localhost:8080/eventos/listar,路径不对也会触发404。

  • 验证pom.xml的web依赖
    确保你的pom.xml里已经引入了Spring Boot的web starter依赖,没有它的话Spring Boot不会启动Tomcat这类web容器,接口自然访问不了:

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

按上面的步骤排查下来,基本就能解决你的问题啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:40:52