Spring MVC Web应用在Tomcat中无法运行,WebApplicationInitializer未触发求助
Spring MVC 部署Tomcat 10后WebApplicationInitializer未触发导致404问题
问题概况
部署环境:Tomcat 10.0.27、VSCode、Java 18
- 搭建的Hello World Spring MVC应用,执行
mvn clean install生成war包部署到Tomcat后,访问http://localhost:8080/springmvctest/test返回404错误 - Tomcat控制台无任何报错信息,也没有Spring框架的初始化日志,WebApplicationInitializer实现类的
onStartup方法未执行(未打印预设的"hi")
核心原因
- 规范版本不兼容:Tomcat 10基于Jakarta EE 9规范,API包名从
javax.servlet改为jakarta.servlet;而你使用的Spring Boot 2.6.4依赖的Spring 5.x基于传统Java EE规范,两者API不兼容,导致Tomcat无法识别Spring的WebApplicationInitializer实现类。 - pom.xml配置混乱:混合了原生Spring和Spring Boot依赖,且
sourceDirectory设置错误(指定为src/main/而非默认的src/main/java,会导致编译时无法正确识别Java类)。 - 非法包名:使用
java作为自定义类的包名,这是Java系统预留包名,可能导致类加载器限制,无法正常加载你的配置类和初始化类。
解决方案
1. 升级Spring版本适配Tomcat 10
Tomcat 10要求使用基于Jakarta EE的Spring版本:
- 若使用Spring Boot,升级到3.x版本(最低支持Java 17);
- 若使用原生Spring MVC,升级到Spring 6.x版本,并引入
jakarta.servlet相关依赖。
2. 修正pom.xml配置
- 移除错误的
sourceDirectory配置,默认src/main/java即可; - 清理冗余依赖(如单独的
spring-aop,spring-boot-starter-web已包含相关依赖); - 统一使用Spring Boot 3.x的parent管理依赖版本。
3. 更换合法包名
将所有类的包名从java改为自定义包名(如com.example),避免系统包名限制。
4. 使用Spring Boot标准的Servlet初始化方式
Spring Boot提供了SpringBootServletInitializer,替代原生的WebApplicationInitializer,更适配war包部署场景。
修正后的关键代码示例
修正后的pom.xml(Spring Boot 3.x)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>springmvctest</artifactId> <version>1.0</version> <packaging>war</packaging> <name>springmvctest</name> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>springmvctest</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
修正后的初始化类WebInit
package com.example; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class WebInit extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebConfig.class); } }
修正后的WebConfig配置类
package com.example; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan("com.example") public class WebConfig { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
修正后的TestController
package com.example; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") public class TestController { @GetMapping("/test") public String testMethod(Model model) { model.addAttribute("greeting", "Hello World 2"); return "welcome"; } }
内容的提问来源于stack exchange,提问作者Shadow504
相关产品推荐
相关产品推荐

