本文共 8689 字,大约阅读时间需要 28 分钟。
官方文档链接:
软件 | 开发语言&框架 | 状态 | 最后版本 | 风格 |
---|---|---|---|---|
Knife4j | Java、JavaScript、Vue | 持续更新中 | 无 | 1.9.6版本是蓝色,后续版本为黑色 |
swagger-bootstrap-ui | Java、JavaScript、jQuery | 停更 | 1.9.6 | 蓝色 |
版本 | 说明 |
---|---|
1.9.6 | 蓝色皮肤风格,增加更多后端模块 |
2.0~2.0.5 | Ui重写,蓝色背景变成黑色,底层依赖的springfox框架版本是2.9.2 |
2.0.6~ | 底层springfox框架版本升级知2.10.5,OpenAPI规范是v2 |
3.0~ | 底层依赖springfox框架版本升级至3.0.3,OpenAPI规范是v3 |
(1) 添加依赖
com.github.xiaoymin knife4j-spring-boot-starter ${knife4j.version}
(2) 编写配置
package com.ruyidan.knife4j.config;import java.util.ArrayList;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.core.env.Profiles;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;import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;/** * @ClassName: SwaggerConfig * @Description: * @Author: dangbo * @Date: 2021/3/31 14:13 * @Version */@Configuration@EnableSwagger2WebMvc// @EnableKnife4j // 因为在配置文件中配置,因此不需要这个注解了public class Knife4jConfig { @Autowired private Environment environment; @Bean public Docket docket() { // 设置显示的swagger环境信息 Profiles profiles = Profiles.of("dev", "test"); // 判断是否处在自己设定的环境当中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("分组名称") // 配置api文档的分组 .enable(flag) // 配置是否开启swagger .select() .apis(RequestHandlerSelectors.basePackage("com.ruyidan")) //配置扫描路径 .paths(PathSelectors.any()) // 配置过滤哪些 .build(); } // api基本信息 private ApiInfo apiInfo() { return new ApiInfo("dxiaodang's swagger", "测试swagger-ui", "v1.0", "http://mail.qq.com", new Contact("dangbo", "http://mail.qq.com", "145xxxxx@qq.com"), //作者信息 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); }}
(3) 访问测试
---------------------------------------------------------------------------------------------------------------------------------------------
简单入门的springboot整合knife4j就完成了,如果不需要使用增强功能,到此就结束了;后续介绍常用的Kni4j的增强功能!!!---------------------------------------------------------------------------------------------------------------------------------------------(4) 开启增强功能
knife4j 1.9.6版本不支持增强功能;之后的版本才支持增强功能;
knife4j 2.0.6及以上版本,Spring Boot的版本必须大于等于2.2.x,且springfox版本要对应;
(第一步)
(第二步)
package com.ruyidan.knife4j.config;import java.util.ArrayList;import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.core.env.Profiles;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;import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;/** * @ClassName: SwaggerConfig * @Description: * @Author: dangbo * @Date: 2021/3/31 14:13 * @Version */@Configuration@EnableSwagger2WebMvc@EnableKnife4jpublic class Knife4jConfig { /*引入Knife4j提供的扩展类*/ @Autowired private OpenApiExtensionResolver openApiExtensionResolver; @Autowired private Environment environment; @Bean public Docket docket() { // 设置显示的swagger环境信息 Profiles profiles = Profiles.of("dev", "test"); // 判断是否处在自己设定的环境当中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("dxiaodang's group") // 配置api文档的分组 .enable(flag) // 配置是否开启swagger .select() .apis(RequestHandlerSelectors.basePackage("com.ruyidan")) //配置扫描路径 .paths(PathSelectors.any()) // 配置过滤哪些 .build() // 为了区分开发者在构建自定义文档时,在不同的Docket逻辑分组下进行区别显示 .extensions(openApiExtensionResolver.buildExtensions("md")); } // api基本信息 private ApiInfo apiInfo() { return new ApiInfo("dxiaodang's swagger", "测试knife4j-ui", "v1.0", "http://mail.qq.com", new Contact("dangbo", "http://mail.qq.com", "145xxxxx@qq.com"), //作者信息 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); }}
(5) 编写配置
knife4j: enable: true # 是否开启Knife4j增强模式,默认值为false cors: false # 是否开启一个默认的跨域配置,该功能配合自定义Host使用,默认值为false production: false # 对Knife4j提供的资源提供BasicHttp校验,保护文档 basic: enable: true # 关闭BasicHttp功能,默认为false username: dangbo # basic用户名 password: dangbo # basic密码 documents: # 自定义文档集合,该属性是数组 - group: md版本 # 所属分组 name: 测试分组1 # 类似于接口中的tag,对于自定义文档的分组 locations: classpath:md/* # markdown文件路径,可以是一个文件夹(classpath:markdowns/*),也可以是单个文件(classpath:md/sign.md) - group: markdown版本 name: 测试分组2 locations: classpath:markdown/* setting: # 前端Ui的个性化配置属性 language: en-US # Ui默认显示语言,目前主要有两种:中文(zh-CN)、英文(en-US),默认为中文 enableSwaggerModels: true # 是否显示界面中SwaggerModel功能,默认是true enableDocumentManage: true # 是否显示界面中"文档管理"功能,默认是true swaggerModelName: 实体类列表 # 重命名SwaggerModel名称 enableVersion: false # 是否开启界面中对某接口的版本控制,如果开启,后端变化后Ui界面会存在小蓝点 enableReloadCacheParameter: false # 是否在每个Debug调试栏后显示刷新变量按钮,默认不显示 enableAfterScript: true # 调试Tab是否显示AfterScript功能,默认开启 enableFilterMultipartApiMethodType: POST # 具体接口的过滤类型,默认为POST enableFilterMultipartApis: false # 针对RequestMapping的接口请求类型,在不指定参数类型的情况下,如果不过滤,默认会显示7个类型的接口地址参数,如果开启此配置,默认展示一个Post类型的接口地址 enableRequestCache: true # 是否开启请求参数缓存 enableHost: false # 是否启用Host,默认为false enableHostText: 192.168.0.193:8000 # 主机描述 enableHomeCustom: true # 是否开启自定义主页内容,默认为false homeCustomLocation: classpath:markdown/home.md # 主页内容Markdown文件路径 enableSearch: false # 是否禁用Ui界面中的搜索框,默认为false enableFooter: false # 是否显示Footer,默认为true enableFooterCustom: true # 是否开启自定义Footer,默认为false footerCustomContent: Apache License 2.0 # 自定义Footer内容 enableDynamicParameter: false # 是否开启动态参数调试功能,默认为false enableDebug: true # 启用调试,默认为true enableOpenApi: false # 显示OpenAPI规范,默认为true enableGroup: true # 显示服务分组,默认为true
(6) 验证增强功能(列举几个,想尝试的朋友可以看看)
---------------------------------------------------------------------------------------------------------------------------------------------
如果大家想看Spring Cloud整合Knife4j,可以留言,我花时间整理下分享给大家本次分享就到这儿了,如果有疑问的话,欢迎大家一起探讨!转载地址:http://lwbuz.baihongyu.com/