解决方案之‘Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoi

文章介绍了在SpringBoot2.6.x版本上集成Swagger时遇到的FailedtostartbeandocumentationPluginsBootstrapper;nestedexceptionisjava.lang.NullPointerException错误。问题源于Springfox的路径匹配与SpringBoot的新路径匹配机制不兼容。提供了两种修复方案:一是降级SpringBoot版本至2.6.x以下;二是保持SpringBoot版本不变,通过配置Springmvc.pathmatch.matching-strategy为ant_path_matcher并创建自定义JavaConfig来解决冲突。
摘要由CSDN通过智能技术生成

'Failed to start bean documentationPluginsBootstrapper'; nested exception is java.lang.NullPointe

1.Bug现场

2. 问题分析

3.修复方案

3.1 修复方案一:降低Spring Boot 版本到2.6.x以下版本

3.2 修复方案二: SpringBoot版本不降级解决方案

1. Bug现场

当Spring Boot 2.6.x 和Swagger 整合的时候,可能会报错如下所示:


org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

2. 问题分析


因为Springfox 使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。

3. 修复方案

3.1 修复方案一:降低Spring Boot 版本到2.6.x以下版本

比如下面版本组合是兼容的

Spring Boot版本

Swagger 版本

2.5.6

2.9.2/3.0.0

3.2 修复方案二: SpringBoot版本不降级解决方案

比如下面版本组合

Spring Boot版本

Swagger 版本

2.6.X

2.9.2/3.0.0


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

第一步:

application.yml 或applicaiton.properties 中添加如下配置


 spring.mvc.pathmatch.matching-strategy=ant_path_matcher

第二步:

项目中新建一个Java Config 类,内容如下:


/**
 *
 */
 
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    @Bean
    public Docket docket(){
        Docket docket  = new Docket(DocumentationType.SWAGGER_2);
        //设置apiInfo
        docket = docket.apiInfo(new ApiInfoBuilder()
                .title("某某项目")
                .version("1.0.0")
                .description("基于SpringBoot、MyBatisPlus、Redis、Vue的前后端分离项目")
                .build());

        //那些controller参与api生成
        docket = docket.select().apis(RequestHandlerSelectors
                .basePackage("com.example.web.controller")).build();

        return docket;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值