【常见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 道阻且长,行则将至,让我们一起加油吧!???

相关推荐
Kiri霧2 分钟前
Range循环和切片
前端·后端·学习·golang
WizLC5 分钟前
【Java】各种IO流知识详解
java·开发语言·后端·spring·intellij idea
Mr.朱鹏7 分钟前
SQL深度分页问题案例实战
java·数据库·spring boot·sql·spring·spring cloud·kafka
Victor35611 分钟前
Netty(19)Netty的性能优化手段有哪些?
后端
爬山算法18 分钟前
Netty(15)Netty的线程模型是什么?它有哪些线程池类型?
java·后端
白宇横流学长1 小时前
基于SpringBoot实现的冬奥会科普平台设计与实现【源码+文档】
java·spring boot·后端
Python编程学习圈1 小时前
Asciinema - 终端日志记录神器,开发者的福音
后端
bing.shao1 小时前
Golang 高并发秒杀系统踩坑
开发语言·后端·golang
壹方秘境2 小时前
一款方便Java开发者在IDEA中抓包分析调试接口的插件
后端
brzhang2 小时前
A2UI:但 Google 把它写成协议后,模型和交互的最后一公里被彻底补全
前端·后端·架构