【常见BUG】Spring Boot 和 Springfox(Swagger)版本兼容问题

???欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。

非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。??? 欢迎订阅本专栏

博客目录

一.报错信息

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

二.解决方案

根据提供的错误信息和搜索结果,这个问题通常与 Spring Boot 和 Springfox(Swagger)的集成有关。错误提示Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException表明在 Spring Boot 应用启动过程中,documentationPluginsBootstrapper这个 bean 无法正常启动,原因是遇到了空指针异常(NullPointerException)。这通常是由于 Spring Boot 和 Springfox 的版本不兼容导致的路径匹配策略冲突。

1.修改 Spring MVC 的路径匹配策略

修改 Spring MVC 的路径匹配策略 :Springfox 假设 Spring MVC 的路径匹配策略是ant-path-matcher,而 Spring Boot 2.6 及以上版本的默认匹配策略是path-pattern-matcher。您可以通过在application.ymlapplication.properties配置文件中添加以下配置来解决这个问题:

复制代码
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

这样可以将 Spring MVC 的路径匹配策略更改为ant-path-matcher,以兼容 Springfox 的要求。

2.配置 WebMvcConfigurer

配置 WebMvcConfigurer :您可以通过创建一个配置类并继承WebMvcConfigurationSupport,然后重写addResourceHandlers方法来解决静态资源路径问题:

复制代码
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

这样可以确保 Swagger 的静态资源能够被正确加载。

3.检查依赖关系

检查依赖关系 :确保您的项目中包含了正确的 Spring Boot Actuator 依赖。如果您使用的是 Maven,可以在pom.xml文件中添加以下依赖:

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

这有助于确保documentationPluginsBootstrapper bean 能够正确创建。

4.降低 SpringBoot 版本
复制代码
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.6</version>
  <relativePath/>
</parent>

觉得有用的话点个赞 ???? 呗。

本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!???

???如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!?? ?? ??

???Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!???

相关推荐
Pedantic1 小时前
SwiftUI 手势笔记
前端·后端
金銀銅鐵1 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
飘尘4 小时前
前端转型全栈(Java后端)的快速上手指引
前端·后端·全栈
浏览器工程师5 小时前
AI Agent 接浏览器任务,先别让它一路点到底
前端·后端
行者全栈架构师5 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
Chenyiax5 小时前
从一次请求看懂 OkHttp:架构、调度与连接管理
后端
爱勇宝6 小时前
深扒 Anthropic 1680 位工程师简历:应届生几乎没机会,AI 公司最缺的不是博士
前端·后端·程序员
AskHarries6 小时前
工具失败时怎么办:重试、回滚、人工确认和风险提示
后端·程序员
苏三说技术7 小时前
Claude Code从失控到起飞,只用了这些技巧
后端