这几日在项目中,老是看到看到许多swagger注解,许多只能了解各大概,抱着得过且过的态度用用就行了。直到自己在构建项目时,发现单纯的添加依赖会报空指针错误。

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

一番百度后发现,Springfox假设MVC的路径匹配政策是ant-path-matcher,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher,这就造成了上面的报错。

依赖

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

配置注解

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
ApiInfo apiInfo =
new ApiInfoBuilder()
.title("5101Club日常管理系统API——Gateway")
.description("管理系统采用SpringBoot开发,API文档集成Swagger")
.version("1.0")
.contact(new Contact("slipperySoap", "https://github.com/XXDBOOM", "xxdboom@outlook.com"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
// 配置swagger显示的controller,如果不配置则默认扫描所有后端接口
.apis(RequestHandlerSelectors.basePackage("com.xxd.testdemo.controller"))
.paths(PathSelectors.any())
.build();
}
}

配置文件

spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher

运行测试

result