运行Spring Boot 3.3最佳实践Recipe遇ClassNotFoundException求助
问题原因分析
从你提供的嵌套Recipe日志可以看到,org.openrewrite.java.spring.boot3.SpringBoot33BestPractices 内部调用了 org.openrewrite.java.springdoc.UpgradeSpringDoc_2,而这个Recipe的核心逻辑是通过 org.openrewrite.java.dependencies.UpgradeDependencyVersion 将SpringDoc版本直接设为 2.x——这会自动拉取最新的2.x版本(即v2.8.9),但没有关联Spring Boot的版本兼容性约束。
根据SpringDoc的兼容性矩阵,Spring Boot 3.3.x 仅适配SpringDoc 2.6.x版本,而v2.8.9依赖的org.springframework.web.servlet.resource.LiteWebJarsResourceResolver 是Spring Framework 6.2.0新增的类,该框架版本仅支持Spring Boot 3.4.0及以上,因此出现ClassNotFoundException。
可行解决方案
1. 自定义Recipe替换内置SpringDoc升级逻辑
不直接使用SpringBoot33BestPractices,而是组合官方的Spring Boot升级Recipe和自定义的SpringDoc兼容版本升级Recipe:
- 创建自定义Recipe文件(比如
upgrade-springdoc-for-boot33.yml):
type: specs.openrewrite.org/v1beta/recipe name: com.yourorg.UpgradeSpringDocForBoot33 displayName: 为Spring Boot 3.3.x升级兼容的SpringDoc版本 description: 匹配Spring Boot 3.3.x兼容性矩阵,将SpringDoc升级至2.6.x系列最新版本 recipeList: - org.openrewrite.java.dependencies.UpgradeDependencyVersion: groupId: org.springdoc artifactId: "*" newVersion: "2.6.x" overrideManagedVersion: true
- 在Rewrite插件配置中,用
UpgradeSpringBoot_3_3搭配自定义Recipe:
以Maven为例:
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.40.0</version> <configuration> <activeRecipes> <recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_3</recipe> <recipe>com.yourorg.UpgradeSpringDocForBoot33</recipe> </activeRecipes> </configuration> <dependencies> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>5.40.0</version> </dependency> <!-- 引入自定义Recipe所在的依赖,若为本地文件可通过system依赖配置 --> </dependencies> </plugin>
2. 禁用内置SpringDoc升级Recipe并补充兼容版本升级
如果仍想保留SpringBoot33BestPractices的其他优化,可以排除其中的SpringDoc升级逻辑,再单独添加兼容的版本升级:
以Maven为例:
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.40.0</version> <configuration> <activeRecipes> <recipe>org.openrewrite.java.spring.boot3.SpringBoot33BestPractices</recipe> <recipe>com.yourorg.UpgradeSpringDocForBoot33</recipe> </activeRecipes> <excludedRecipes> <recipe>org.openrewrite.java.springdoc.UpgradeSpringDoc_2</recipe> </excludedRecipes> </configuration> <dependencies> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>5.40.0</version> </dependency> </dependencies> </plugin>
3. 向OpenRewrite社区反馈兼容性问题
这属于官方Recipe的逻辑疏漏,你可以在OpenRewrite的GitHub仓库提交Issue,说明SpringBoot33BestPractices未遵循SpringDoc与Spring Boot的兼容性矩阵,请求优化Recipe逻辑,实现根据目标Spring Boot版本自动匹配对应兼容的SpringDoc版本。
内容的提问来源于stack exchange,提问作者Jesus Eiroa

