Knife4j简单使用
导入依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索3.X最新版本号-->
<version>3.0.2</version>
</dependency>
编写配置类:
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.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo()).enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.star.nightstar.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("nightstar后端API文档")
.description("")
.contact(new Contact("测试","",""))
.version("1.0")
.build();
}
}
启动项目,访问
http://localhost:8080/doc.html
遇到的问题,启动项目时,报了如下错误:
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
原因是因为springboot项目版本过高
修改application.yml文件配置:
spring:
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
问题解决参考:
https://blog.csdn.net/lck_csdn/article/details/124289428