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

Spring+Liquibase+PostgreSQL应用404 Not Found错误求助

404 Not Found 问题排查方案

一、Controller 核心配置检查

  • 确认Controller类是否添加了正确的请求处理注解:必须标注@RestController(或@Controller+@ResponseBody),否则Spring无法将其识别为请求处理类。示例:
    @RestController
    @RequestMapping("/api")
    public class PersonController {
        @GetMapping("/persons")
        public List<Person> getAllPersons() {
            // 业务逻辑
        }
    }
    
  • 核对路径拼写:类上的@RequestMapping("/api")与方法上的/persons需组合成/api/persons,检查是否存在拼写错误(比如少写s、多打斜杠)。
  • 匹配请求方法:如果Controller用了@GetMapping,就不能用POST请求调用,反之亦然,确保请求方式和注解完全匹配。

二、Spring 组件扫描验证

  • 检查主启动类位置:带@SpringBootApplication的主类必须位于Controller、Repository所在包的上层目录。比如主类在com.example.demo,Controller需放在com.example.demo.controller下,不能在com.example.controller(层级高于主类会导致扫描失败)。
  • 若包结构不符合默认规则,手动指定扫描范围:
    @SpringBootApplication(scanBasePackages = {"com.example.demo.controller", "com.example.demo.repository"})
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

三、Web 路径匹配配置排查

  • 检查application.properties中的路径匹配规则:如果存在以下配置,可能导致路径匹配异常,可注释后重试:
    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    
  • 排查自定义WebMvcConfigurer:若有自定义配置类,检查是否重写了addResourceHandlers或configurePathMatch方法,避免请求路径被意外拦截或修改。

四、Docker 容器化配置检查

  • 核对端口映射:查看docker-compose.yml中Spring应用的端口映射是否正确,确保调用的宿主机端口和配置一致:
    services:
      spring-app:
        ports:
          - "8080:8080" # 宿主机端口:容器内端口,需和你调用的端口匹配
    
  • 查看容器启动日志:执行docker logs <spring容器名称>,确认是否存在Tomcat started on port(s): 8080 (http)日志,同时检查是否有Controller映射日志(比如Mapped "{[/api/persons],methods=[GET]}" onto public ...),若没有这条日志,说明Spring未识别到Controller。

五、实体与Repository 关联确认(辅助排查)

  • 检查Person实体类注解:确认@Entity、@Table(name = "person")的表名与Liquibase创建的表完全一致(PostgreSQL默认表名小写,注意大小写匹配)。
  • 验证Repository接口:确保继承了JpaRepository/CrudRepository,且标注@Repository注解(Spring会自动代理,但避免拼写错误):
    @Repository
    public interface PersonRepository extends JpaRepository<Person, Long> {
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:47:14