Java17迁移后Spring Boot类路径兼容问题:spring-web与autoconfigure版本冲突
解决Spring Boot 2.7.11与Spring Web版本冲突及运行时错误
为修复高危漏洞,我将API从Java 11迁移至Java 17,同时把Spring Boot版本从2.7.5升级到2.7.11。应用构建正常,但运行时出现错误:org.springframework.boot.autoconfigure.condition.OnWarDeploymentCondition调用org.springframework.web.context.WebApplicationContext.getServletContext()方法不存在。排查发现是项目中同时使用spring-boot-autoconfigure 2.7.12与spring-web 6.0.0导致版本不兼容。尝试过调整多个版本的spring-web和spring-boot-autoconfigure,其中spring-web 5.3.27可正常运行但存在高危漏洞,Stack Overflow上的方案也无效。以下是我的Gradle依赖配置:
plugins { id 'org.springframework.boot' version '2.7.11' id 'io.spring.dependency-management' version '1.0.15.RELEASE' id 'java' } java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } repositories { mavenCentral() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.mockito:mockito-core:4.8.0' testImplementation 'org.mockito:mockito-inline:4.8.0' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springdoc:springdoc-openapi-ui:1.6.12' implementation 'redis.clients:jedis:4.3.1' implementation 'com.google.code.gson:gson:2.10' implementation platform('com.amazonaws:aws-java-sdk-bom:1.12.328') implementation 'com.amazonaws:aws-java-sdk-dynamodb:1.12.332' implementation 'software.amazon.awssdk:dynamodb:2.20.0' implementation 'software.amazon.awssdk:athena:2.20.5' implementation 'org.opensearch:opensearch:2.6.0' implementation 'org.opensearch.client:opensearch-rest-high-level-client:2.3.0' implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4' implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2' implementation 'com.amazonaws:aws-java-sdk-sqs:1.12.333' implementation 'org.springframework.boot:spring-boot-starter-aop' implementation 'org.springframework.boot:spring-boot-starter-parent:3.2.0' implementation 'org.apache.httpcomponents.client5:httpclient5-fluent:5.1.4' implementation 'org.springframework.boot:spring-boot-starter-webflux' compileOnly 'org.projectlombok:lombok:1.18.24' implementation group: 'commons-io', name: 'commons-io', version: '2.7' annotationProcessor 'org.projectlombok:lombok:1.18.24' annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-autoconfigure:2.7.12' implementation 'org.springframework:spring-core' implementation 'org.springframework:spring-web:6.0.0' implementation 'com.mysql:mysql-connector-j' implementation 'ch.qos.logback:logback-classic:1.2.13' implementation 'ch.qos.logback:logback-core:1.2.13' implementation 'org.postgresql:postgresql:42.7.0' implementation 'commons-dbutils:commons-dbutils:1.8.1' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'io.netty:netty-all:4.1.100.Final' implementation 'io.netty:netty-codec-haproxy:4.1.100.Final' implementation 'io.netty:netty-codec-http:4.1.100.Final' implementation 'io.netty:netty-codec-http2:4.1.100.Final' implementation 'io.netty:netty-handler:4.1.100.Final' implementation 'io.projectreactor.netty:reactor-netty-http:1.1.13' implementation 'org.jooq:jooq:3.18.7' implementation 'org.jooq:jooq-codegen:3.18.7' implementation 'org.jooq:jooq-meta:3.18.7' implementation 'org.json:json:20231013' implementation 'org.apache.tomcat.embed:tomcat-embed-core:9.0.83' implementation 'javax.annotation:javax.annotation-api:1.3.2' implementation 'javax.xml.bind:jaxb-api:2.3.1' implementation 'org.eclipse.jetty:jetty-server:11.0.14' implementation 'org.springframework.boot:spring-boot-starter-jetty:3.1.2' implementation 'org.apache.commons:commons-collections4:4.4' implementation 'com.amazonaws:aws-java-sdk-ssm' implementation 'org.yaml:snakeyaml:2.0' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.github.resilience4j:resilience4j-spring-boot2:1.7.1' implementation 'io.github.resilience4j:resilience4j-reactor:1.7.1' testImplementation 'org.springframework.boot:spring-boot-starter-test:2.6.3' testCompileOnly 'org.projectlombok:lombok:1.18.24' testAnnotationProcessor 'org.projectlombok:lombok:1.18.24' }
核心问题分析
你的依赖配置存在大量版本冲突和冗余:
- 手动指定
spring-web:6.0.0,但Spring Boot 2.7.x对应的Spring Framework版本是5.3.x,6.0.x属于Spring Boot 3.x配套版本,完全不兼容。 - 同时引入
spring-boot-starter-parent:3.2.0和Spring Boot 2.7.11插件,版本混乱。 - 重复添加
spring-boot-starter-webflux,还混用不同版本的Jetty starter(3.1.2与2.7.11不匹配)。 - 手动指定
spring-boot-autoconfigure版本,破坏了Spring Boot的依赖管理机制。
修正后的Gradle配置
plugins { id 'org.springframework.boot' version '2.7.11' id 'io.spring.dependency-management' version '1.0.15.RELEASE' id 'java' } java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } repositories { mavenCentral() } dependencies { // 测试依赖 testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.mockito:mockito-core:4.8.0' testImplementation 'org.mockito:mockito-inline:4.8.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' // Spring Boot核心starter implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-aop' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-jetty' // 第三方工具依赖 implementation 'org.springdoc:springdoc-openapi-ui:1.6.15' implementation 'redis.clients:jedis:4.3.1' implementation 'com.google.code.gson:gson:2.10' implementation 'commons-io:commons-io:2.7' implementation 'org.apache.commons:commons-collections4:4.4' implementation 'org.json:json:20231013' implementation 'ch.qos.logback:logback-classic:1.2.13' // AWS相关依赖 implementation platform('com.amazonaws:aws-java-sdk-bom:1.12.328') implementation 'com.amazonaws:aws-java-sdk-dynamodb' implementation 'com.amazonaws:aws-java-sdk-sqs' implementation 'com.amazonaws:aws-java-sdk-ssm' implementation platform('software.amazon.awssdk:bom:2.20.5') implementation 'software.amazon.awssdk:dynamodb' implementation 'software.amazon.awssdk:athena' // OpenSearch相关 implementation 'org.opensearch:opensearch:2.6.0' implementation 'org.opensearch.client:opensearch-rest-high-level-client:2.6.0' // JOOQ相关 implementation 'org.jooq:jooq:3.18.7' implementation 'org.jooq:jooq-codegen:3.18.7' implementation 'org.jooq:jooq-meta:3.18.7' // 数据库驱动 implementation 'com.mysql:mysql-connector-j' implementation 'org.postgresql:postgresql:42.7.0' implementation 'commons-dbutils:commons-dbutils:1.8.1' // 注解和配置相关 compileOnly 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' // 兼容Java EE相关 implementation 'javax.annotation:javax.annotation-api:1.3.2' implementation 'javax.xml.bind:jaxb-api:2.3.1' // 容错组件 implementation 'io.github.resilience4j:resilience4j-spring-boot2:1.7.1' implementation 'io.github.resilience4j:resilience4j-reactor:1.7.1' // 配置文件解析 implementation 'org.yaml:snakeyaml:2.0' }
关键修改说明
- 移除冲突版本:删掉
spring-web:6.0.0、spring-boot-autoconfigure:2.7.12、spring-boot-starter-parent:3.2.0,让Spring Boot的dependency-management插件自动管理核心依赖版本,保证兼容性。 - 统一第三方版本:AWS SDK v1、v2分别用BOM统一版本;OpenSearch客户端版本与服务端保持一致;springdoc-openapi-ui升级到适配2.7.x的最新版本。
- 清理冗余依赖:去掉重复的
spring-boot-starter-webflux,删除手动指定的netty、jackson依赖(webflux和Spring Boot会自动引入兼容版本)。 - 修正测试依赖:
spring-boot-starter-test使用依赖管理的版本,与主项目版本匹配。
验证步骤
- 执行
./gradlew clean build清理缓存并重新构建。 - 启动应用,检查是否仍存在
getServletContext()不存在的错误。 - 执行
./gradlew dependencies检查依赖树,确认spring-web、spring-boot-autoconfigure版本统一为2.7.11配套版本。
内容的提问来源于stack exchange,提问作者Şafak
相关产品推荐
相关产品推荐

