<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class Knife4jConfig {
@Bean
public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("接口文档")
.description("knife4j-demomo")
.version("1.0")
.build())
.select()
// 指定 Controller 扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.example.knife4jdemo.demos.web"))
.paths(PathSelectors.any())
.build();
}
}
如果启动项目的时候启动失败了,报的错可能是
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
这个错误在springboot版本>=2.6.0的时候会引起,解决办法:
①、降低springboot版本
②、在application.yml中加入
spring:
# 支持 swagger3
mvc:
pathmatch:
matching-strategy: ant_path_matcher