'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;
}
}