升级Spring Boot 1.5.22至2.0.0时org.springframework.http包缺失
解决Spring Boot 2.0升级后
org.springframework.http包不存在的编译错误 以下是针对多模块Gradle项目的排查和解决步骤:
检查依赖传递与模块依赖声明
- 确认报错的模块是否正确依赖了包含
spring-boot-starter-web的模块,或直接在该模块的build.gradle中声明依赖:
注意:Spring Boot 2.0开始,依赖配置已从dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' }compile替换为implementation,旧配置关键字可能导致依赖无法正确传递。 - 执行
./gradlew :[报错模块名]:dependencies查看依赖树,确认spring-web(包含org.springframework.http包的核心依赖)是否被正确引入,排查是否有模块通过exclude规则移除了该依赖。
- 确认报错的模块是否正确依赖了包含
验证Gradle版本兼容性
Spring Boot 2.0.0.RELEASE要求Gradle版本≥4.0,若项目仍使用低版本Gradle,会引发依赖解析异常:- 修改根目录
gradle/wrapper/gradle-wrapper.properties中的distributionUrl,指定Gradle 4.x版本,例如:distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
- 修改根目录
清理Gradle缓存并强制刷新依赖
旧缓存可能导致依赖冲突,执行以下命令清理缓存并重新构建:./gradlew clean build --refresh-dependencies排查并解决依赖冲突
执行./gradlew :[报错模块名]:dependencyInsight --dependency spring-web,查看是否有其他依赖引入了旧版本spring-web导致版本冲突。若存在冲突,可在根build.gradle的dependencyManagement块中统一管理版本:dependencyManagement { imports { mavenBom "org.springframework.boot:spring-boot-dependencies:2.0.0.RELEASE" } }
内容的提问来源于stack exchange,提问作者Manish Kapoor
相关产品推荐
相关产品推荐

