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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀死一只知更鸟debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值