Spring Boot项目无法访问jakarta.servlet.FilterChain类版本兼容问题求助
解决方案
1. 移除外部Tomcat 11的类路径引用
Spring Boot自带嵌入式Tomcat,无需手动添加外部Tomcat的类库到项目类路径:
- 打开IntelliJ的Run/Debug Configurations,找到你的Spring Boot启动配置,切换到
Classpath标签,检查并移除所有指向C:\Program Files\Apache Software Foundation\Tomcat 11.0\lib的条目。 - 进入Project Structure → Libraries,如果存在Tomcat 11相关的库,直接删除。
2. 修复依赖版本混乱问题
你的pom.xml存在多版本Spring Boot组件冲突,这是核心问题之一:
- 添加Spring Boot Parent依赖统一版本管理,放在
<dependencies>之前:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> </parent> - 删除以下依赖的手动版本声明,让Parent自动管理兼容版本:
spring-boot-starter-mail的versionspring-security-config的versiontomcat-embed-core的version
- 移除手动添加的
jakarta.servlet-api依赖,因为spring-boot-starter-web已经包含了适配版本的Servlet API,重复添加会导致冲突。
3. 匹配JDK版本
Spring Boot 2.4.x支持JDK 8/11/16,而Tomcat 11的类文件是JDK 21编译的(版本65),完全不兼容:
- 在IntelliJ中设置项目JDK:File → Project Structure → Project,将
Project SDK设为JDK 8或11,Project language level对应设置为8或11。 - 在pom.xml中添加Maven编译配置,确保编译版本一致:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <!-- 对应你的JDK版本,比如8 --> <target>11</target> </configuration> </plugin> </plugins> </build>
4. 重新构建项目
- 执行Maven命令:
mvn clean install -U,强制更新依赖并清理旧编译文件。 - 重启IntelliJ:File → Invalidate Caches... → Invalidate and Restart,清除IDE缓存。
内容的提问来源于stack exchange,提问作者swathi raju
相关产品推荐
相关产品推荐

