Spring MVC项目部署Tomcat9遇ClassNotFoundException问题求助
问题:Spring MVC部署Tomcat 9时出现
java.lang.ClassNotFoundException: jakarta.servlet.ServletException 我正在开发一个Spring MVC Web应用,使用Maven(Webapp原型)、Spring 5、Java 17和Tomcat 9.0;部署工件时遇到java.lang.ClassNotFoundException: jakarta.servlet.ServletException异常,明明Tomcat 9.0是基于javax包的,但在Tomcat 10.x上部署时应用运行完全正常。
以下是我的pom.xml:
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring-mvc-demo</artifactId> <packaging>war</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <version>1.0-SNAPSHOT</version> <name>spring-mvc-demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>6.0.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-indexer --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <version>5.3.25</version> <scope>optional</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-r2dbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-r2dbc</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.25</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>6.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webflux --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>5.3.25</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>spring-mvc-demo</finalName> </build> </project>
解决方案
问题根源
你的pom.xml中混合了Spring 5和Spring 6的依赖:
- Spring 5基于Java EE规范,依赖
javax.*包,适配Tomcat 9等传统Java EE容器 - Spring 6切换到Jakarta EE规范,依赖
jakarta.*包,仅适配Tomcat 10+等Jakarta EE容器
其中spring-instrument(6.0.5)和spring-web(6.0.5)是Spring 6版本,它们会引入对jakarta.servlet.ServletException的依赖,而Tomcat 9中只有javax.servlet.ServletException,因此抛出类找不到异常。
修改步骤
将所有Spring依赖统一为Spring 5.3.25版本,具体修改以下两个依赖:
- 修正
spring-instrument版本(注意pom中重复引入了该依赖,保留一个即可):
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>5.3.25</version> </dependency>
- 修正
spring-web版本:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.25</version> </dependency>
额外建议
- 可以在pom.xml中添加Spring版本属性,统一管理所有Spring依赖版本,避免手动维护出错:
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <spring.version>5.3.25</spring.version> </properties>
然后所有Spring依赖的version都替换为${spring.version}即可。
- 另外,JUnit 3.8.1版本过旧,建议升级到JUnit 5,不过这和当前问题无关。
内容的提问来源于stack exchange,提问作者user14269959
相关产品推荐
相关产品推荐

