Knife4j是一种基于Swagger的API文档在线编辑工具。它是Swagger的一个增强版本,旨在简化和改善API文档的创建和管理。Knife4j提供了更多交互式的功能,比如在线调试接口、参数的动态修改和调整等,使得API文档的编写和使用更加方便和友好。它可以帮助开发者更轻松地管理和分享API文档,提升团队协作效率。
1、导入依赖
<!-- Knife4j Spring Boot Starter -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2、配置关于Spring框架对Swagger 3的支持以及路径匹配策略的配置。
spring:
# 支持 swagger3
mvc:
pathmatch:
matching-strategy: ant_path_matcher
3、编写Knife4jConfig配置类
@Configuration
@EnableKnife4j
public class Knife4jConfig {
@Bean
public Docket defaultApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("接口文档")
.description("springboot-init")
.version("1.0")
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jayschen.controller"))//改成你自己controller包的路径
.paths(PathSelectors.any())
.build();
}
}
4、访问地址
(1)接口文档地址
localhost:8080/doc.html
(2)json格式文档的地址
localhost:8080/v2/api-docs