Knife4j 概述
Knife4j 是一款基于 Swagger 的 API 文档增强工具,旨在为开发者提供更强大、更易用的 API 文档体验。它继承了 Swagger 的所有功能,并在此基础上进行了全方位的增强,包括界面美化、功能扩展、性能优化等,是构建 API 文档的利器。
Knife4j 的核心优势:
- 界面更美观,体验更友好: Knife4j 提供了更加简洁、美观的界面,并支持多种主题切换,让 API 文档阅读体验更加舒适。同时,Knife4j 还优化了交互逻辑,使得 API 文档的浏览和调试更加便捷。
- 功能更强大,满足更多需求: Knife4j 在 Swagger 的基础上,新增了许多实用功能,例如:
- 离线文档导出: 支持将 API 文档导出为 HTML、Markdown、Word 等多种格式,方便离线查阅和分享。
- 全局参数配置: 支持配置全局参数,例如请求头、认证信息等,简化 API 调试流程。
- 接口分组管理: 支持对接口进行分组管理,方便 API 文档的组织和查阅。
- Mock 数据生成: 支持根据 API 定义自动生成 Mock 数据,方便前端开发和测试。
- 性能更优异,运行更流畅: Knife4j 对 Swagger 的底层代码进行了优化,提升了 API 文档的加载速度和渲染效率,即使面对海量 API 也能保持流畅运行。
Knife4j 的应用场景:
- API 文档生成: Knife4j 可以快速生成美观、易用的 API 文档,方便前后端开发人员查阅和调试。
- API 测试: Knife4j 提供了强大的 API 调试功能,可以方便地进行 API 测试和验证。
- Mock 数据生成: Knife4j 可以根据 API 定义自动生成 Mock 数据,方便前端开发和测试。
- API 管理: Knife4j 提供了接口分组、全局参数配置等功能,方便 API 的管理和维护。
Knife4j 的生态支持:
- 多语言支持: Knife4j 支持 Java、.NET、PHP、Python 等多种编程语言。
- 多框架集成: Knife4j 可以与 Spring Boot、Spring Cloud、Dubbo 等主流框架无缝集成。
- 活跃的社区: Knife4j 拥有活跃的社区,提供丰富的文档和教程,方便开发者学习和使用。
参考文档
参考 官方文档
目录
一、api-doc模块添加pom依赖
<dependencies>
<!-- springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- knife4j接口文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-aggregation-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>

二、配置yml
server:
port: 10909
knife4j:
enableAggregation: true
nacos:
enable: true
serviceUrl: http://localhost:8848/nacos
routeAuth:
enable: true
username: nacos
password: nacos
routes:
- name: 订单服务
serviceName: service-order
location: /v2/api-docs?group=default
servicePath: /order/prod

三、主类
package com.aipdoc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DocApplication {
public static void main(String[] args) {
SpringApplication.run(DocApplication.class);
}
}

至此,api-doc模块已经配置好了。如果有gateway网关,要放行api路径,在官方文档中有指引,这里只是做一个简单示例。接下来是在各个微服务模块中配置knife4j。
四、微服务引入pom
<!-- knife4j接口文档 -->
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-micro-spring-boot-starter -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>

五、配置类Knife4jConfiguration
package com.yushanma.config;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfiguration {
@Bean(value = "orderApi")
@Order(value = 1)
public Docket groupRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(groupApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yushanma.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo groupApiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui很棒~~~!!!")
.description("<div style='font-size:14px;color:red;'>swagger-bootstrap-ui-demo RESTful APIs</div>")
.termsOfServiceUrl("http://www.group.com/")
.contact(getContact())
.version("1.0")
.build();
}
private Contact getContact(){
return new Contact("sam","none","1801561529@qq.com");
}
}

注意: RequestHandlerSelectors.basePackage("com.yushanma.controller") 扫描的包名应该是 本微服务下的控制器所在包路径
六、接口组
@Api(tags = "接口组名字")

七、接口与参数
// 编写示例, @ApiOperation指明是什么接口,@ApiImplicitParams指明参数是什么
@ApiOperation(value = "测试接口", notes = "studentMapper与iStudentService的测试")
@ApiImplicitParams({@ApiImplicitParam(name = "msg", required = true, paramType = "path")})

八、测试
先启动其他微服务,最后启动api-doc微服务,访问文档中心 http://localhost:10909/doc.html#/home,我们就可以看到编写好的接口,以及进行在线调试接口


本文介绍如何使用Eureka注册中心聚合多个微服务的OpenAPI文档,并通过具体步骤指导完成配置,包括添加依赖、配置文件调整及接口文档的生成。
1256

被折叠的 条评论
为什么被折叠?



