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

相关推荐
天丁o2 小时前
Spring Boot + MyBatis Plus 考勤日报统计报表:打卡记录聚合、异常分类和明细下钻 Demo
spring boot·mybatis plus·企业数字化·考勤系统·报表统计
Devin~Y2 小时前
从本地生活电商到 AI RAG:互联网大厂 Java 面试场景完整实战
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
米码收割机2 小时前
SA508-3钢回火焊道焊接温度场数值模拟(模拟图+论文)
spring boot·express·宠物
JackSparrow4142 小时前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
geovindu2 小时前
go:loghelper
开发语言·后端·golang
小满zs3 小时前
Go语言第四章(类型转换)
后端·go
米码收割机4 小时前
【项目】spring boot+vue3 宠物领养系统(源码+文档)【独一无二】
java·spring boot·宠物
人间凡尔赛5 小时前
告别冷启动!WebAssembly + Spin 实战:Serverless 延迟从 1 秒降到 1 毫秒
后端·云原生·serverless·webassembly·spin
陈随易11 小时前
bm2,MoonBit实现的pm2替代品
前端·后端·程序员
都叫我大帅哥13 小时前
Spring Data JPA 查询之道:方法命名与示例查询完全指南
java·后端·spring