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

Spring Boot WAR包部署Tomcat后路由失效、控制器未触发问题求助

Spring Boot WAR包部署Tomcat后路由失效排查方案

1. 确认启动类配置

WAR包部署到外部Tomcat时,启动类必须继承SpringBootServletInitializer并实现configure方法,否则Spring上下文无法正确初始化:

@SpringBootApplication
public class YourAppApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(YourAppApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(YourAppApplication.class, args);
    }
}

2. 检查pom.xml关键配置

  • 声明打包类型为WAR:
    <packaging>war</packaging>
    
  • 排除内置Tomcat依赖,避免与外部Tomcat冲突:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    
  • 确保引入PostgreSQL驱动(匹配配置的数据源):
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    
  • 保留Spring Boot Maven插件用于正确打包:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

3. 修正访问路径

部署到Tomcat后,默认会以WAR包名称作为上下文路径。比如你的WAR包名为cghs2test.war,正确访问路径应为:
http://localhost:8080/cghs2test/welcome

  • 若想直接用/welcome访问,可将WAR包重命名为ROOT.war,或在application.properties中添加:
    server.servlet.context-path=/
    

4. 验证控制器配置

  • 确保控制器类标注@Controller或@RestController注解
  • 确保路由注解路径正确,无拼写错误:
    @Controller
    public class WelcomeController {
        @GetMapping("/welcome")
        public String showWelcomePage() {
            return "welcome"; // 若返回视图,需确保templates目录下存在对应视图文件
        }
    }
    

5. 排查日志报错

查看Tomcat日志目录下的catalina.out或localhost.log,重点排查:

  • Spring上下文初始化失败信息
  • PostgreSQL数据源连接错误(比如驱动缺失、端口/数据库名错误)
  • Servlet映射异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 22:52:35