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

相关文章
|
安全 Java Apache
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
629 0
|
安全 Java 数据安全/隐私保护
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
462 0
|
10月前
|
前端开发
SpringBoot2.3.1集成Knife4j接口文档
SpringBoot2.3.1集成Knife4j接口文档
1066 44
|
9月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
582 3
|
9月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
820 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
9月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
898 2
|
9月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
497 2
|
10月前
|
缓存 安全 Java
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
846 7
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
237 0

热门文章

最新文章

下一篇
开通oss服务