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

Spring Boot升级至2.3.0后Tomcat首页404,request.getContext()返回null问题

Spring Boot 1.5.x升级2.3.x后首页404(context为null)排查方案

你遇到的是Spring Boot 2.x版本Tomcat嵌入式上下文初始化失败的典型问题,按以下步骤排查即可定位:

  • 首先核对上下文路径配置
    Spring Boot 2.0开始上下文路径的配置属性从server.context-path变更为server.servlet.context-path,如果项目仍保留旧版本配置,或者配置值存在不规范内容(比如末尾带斜杠、包含特殊符号、配置为空字符串),都会导致Tomcat无法正确注册上下文,请求匹配时context返回null。
  • 核对web依赖和启动类配置
    对比正常运行的升级项目,检查pom.xml/gradle配置:
    1. 确认引入了完整的spring-boot-starter-web依赖,没有额外排除spring-boot-starter-tomcat的配置
    2. 如果是打war包部署的项目,需要保证启动类继承SpringBootServletInitializer并重写configure方法:
@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(YourApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}
  • 核对自定义Tomcat配置
    如果项目中存在手动实现的TomcatServletWebServerFactory自定义配置,检查是否有错误的上下文属性设置,比如强制设置contextPath为空、未正确注册上下文资源的逻辑。
  • 核对请求访问路径
    Spring Boot 2.x对请求路径的匹配校验比1.x更严格,确认你访问的路径前缀和配置的server.servlet.context-path完全匹配,多余的斜杠、大小写不一致都会导致上下文匹配失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 12:36:02