springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决

简介: 本文介绍了如何在Spring Boot项目中集成Swagger 2.x和3.0版本,并提供了解决Swagger在Spring Boot中启动失败问题“Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerEx”的方法,包括配置yml文件和Spring Boot版本的降级。

一、集成 swagger 2.9.2

(一)导入依赖坐标

<!--swagger集成-->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

(二)配置docket

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2  // 开启swagger
public class SwaggerConfig {
   

    /*
    * 配置docket
    *       1.通过apis() 指明要扫描的controller的地址,通过paths方法配置路径
    *       2.在apiInfo 中构建文档的基本信息,描述,联系人信息,版本和标题等
    */
    @Bean
    Docket docket(){
   
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.robin.springboot.controller"))
            .paths(PathSelectors.any())
            .build().apiInfo(new ApiInfoBuilder()
                         .description("知更鸟前后端分离接口测试文档")
                         .contact(new Contact("robinDebug",
                                              "https://blog.csdn.net/m0_63622279?type=blog",
                                              "robinDebug@163.com"))
                         .version("v1.0")
                         .title("API测试文档")
                         .license("Apache2.0")
                         .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                         .build());
    }
}

(三)yml 配置 防止Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx 的问题

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

(四)访问地址

访问地址 http://localhost:9090/swagger-ui.html,自己指定的端口号不同,改成自己的端口号即可。

在这里插入图片描述

二、集成 swagger 3.0

(一)导入依赖坐标

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

(二)配置docket

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi  // 开启swagger
public class SwaggerConfig {
   

    /*
    * 配置docket
    *       1.通过apis() 指明要扫描的controller的地址,通过paths方法配置路径
    *       2.在apiInfo 中构建文档的基本信息,描述,联系人信息,版本和标题等
    */
    @Bean
    Docket docket(){
   
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.robin.springboot.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .description("知更鸟前后端分离接口测试文档")
                        .contact(new Contact("robinDebug",
                                "https://blog.csdn.net/m0_63622279?type=blog",
                                "robinDebug@163.com"))
                        .version("v1.0")
                        .title("API测试文档")
                        .license("Apache2.0") // 许可证
                        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0") //许可证链接
                        .build());
    }
}

(三)yml 配置 防止Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx 的问题

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

(四)访问地址

访问地址 : http://localhost:9090/swagger-ui/
在这里插入图片描述

三、Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx的解决

(一)配置yml

因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。该注解可以更改匹配规则。你也可以直接修改配置spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER来更改规则。

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

(二)springboot 版本降级

降低你的spring版本到2.5及以下,就不再会出现上述的问题。

相关文章
|
8月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
535 3
|
8月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
788 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
8月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
848 2
|
8月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
471 2
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
213 0
|
8月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
JSON Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
本文详细介绍了Swagger2的使用方法,包括在Spring Boot项目中的配置与应用。重点讲解了Swagger2中常用的注解,如实体类上的`@ApiModel`和`@ApiModelProperty`,Controller类上的`@Api`、`@ApiOperation`以及参数上的`@ApiParam`等。通过示例代码展示了如何为实体类和接口添加注解,并在页面上生成在线接口文档,实现接口测试。最后总结了Swagger的优势及其在项目开发中的重要性,提供了课程源代码下载链接供学习参考。
881 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的使用
|
缓存 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
1245 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
|
Java Maven 微服务
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的 maven 依赖
在项目中使用Swagger2工具时,需导入Maven依赖。尽管官方最高版本为2.8.0,但其展示效果不够理想且稳定性欠佳。实际开发中常用2.2.2版本,因其稳定且界面友好。以下是围绕2.2.2版本的Maven依赖配置,包括`springfox-swagger2`和`springfox-swagger-ui`两个模块。
547 0