迁移至Spring Boot 3时测试类报java.lang.NoClassDefFoundError错误求助
问题分析与解决方案
错误原因
Spring Boot 3 基于 Jakarta EE 9 构建,所有原javax.servlet相关API已迁移至jakarta.servlet包下。测试类抛出java.lang.NoClassDefFoundError: javax/servlet/DispatcherType,核心原因是依赖中存在仍依赖旧javax.servlet规范的组件,同时测试依赖存在输入错误。
具体修复步骤
1. 修正测试依赖的输入错误
你的testImplementation中存在无效依赖:
testImplementation 'org`your text`.springframework.boot:spring-boot-starter-test'
需修正为正确的Spring Boot测试 starter:
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2. 升级WireMock版本
你使用的wiremock-jre8:2.35.0版本依赖旧的javax.servlet API,不兼容Spring Boot 3。需升级到支持Jakarta EE的WireMock版本(3.x版本groupId变更为org.wiremock):
testImplementation 'org.wiremock:wiremock-jre8:3.3.1'
3. 验证依赖一致性
执行Gradle命令排查是否还有遗留的javax.servlet依赖:
./gradlew dependencies --configuration testRuntimeClasspath | grep javax.servlet
如果仍有输出,说明还有其他依赖引入了旧API,需进一步排查升级对应组件。
修复后的完整依赖块示例
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-security' // OAuth 2.0 Resource Server support implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server' // Lombok implementation 'org.projectlombok:lombok:1.18.26' annotationProcessor 'org.projectlombok:lombok:1.18.26' // Actuator implementation 'org.springframework.boot:spring-boot-starter-actuator' // Micrometer implementation 'io.micrometer:micrometer-registry-prometheus:1.10.6' // Logger implementation 'net.logstash.logback:logstash-logback-encoder:7.3' // Test testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.wiremock:wiremock-jre8:3.3.1' testImplementation 'org.bitbucket.b_c:jose4j:0.7.6' testAnnotationProcessor 'org.projectlombok:lombok:1.18.26' }
内容的提问来源于stack exchange,提问作者Вадим
相关产品推荐
相关产品推荐

