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及以下,就不再会出现上述的问题。

相关文章
|
7月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
445 3
|
7月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
678 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
7月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
746 2
|
7月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
416 2
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
173 0
|
7月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
Java Maven Docker
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
468 0
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
1321 6