注:该文档使用于SpringBoot 2.2.x — 2.5.x

引入依赖

1
2
3
4
5
6
<!--knife4j-spring-boot-starter-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>

Knife4j配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
* User: XiaoFei
* Date: 2022-12-12
* Time: 22:10:39.766
* Email: 1903078434@qq.com
*
* @author XiaoFei
*/
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

@Bean(value = "dockerBean")
public Docket dockerBean() {
//指定使用Swagger2规范
return new Docket(DocumentationType.SWAGGER_2)
//设置文档的一些信息
.apiInfo(new ApiInfoBuilder()
// 接口文档显示标题
.title("xiaofei-utils 接口测试文档")
//描述字段支持Markdown语法
.description("xiaofei-utils 接口测试文档")
//设置服务Url
.termsOfServiceUrl("https://utils.naste.top:1024/")
//设置用户信息
.contact(new Contact("XiaoFei", "https://utils.naste.top:1024/", "1903078434@qq.com"))
//设置执照信息
.license("所有版权归XiaoFei所有")
//设置执照信息URL
.licenseUrl("https://utils.naste.top:1024/")
//设置版本号
.version("1.0")
//构建
.build())
//分组名称
.groupName("xiaofei-utils")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.xiaofei.utils.controller"))
.paths(PathSelectors.any())
.build();
}
}

Knife4j配置文件

部分配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# knife4j文档地址:https://doc.xiaominfo.com/docs/features
knife4j:
# 是否开启增强模式
enable: true
# 增强模式配置
basic:
# basic配置是否开启,默认为false【true:进入接口文档需要下面配置的用户名和密码 false:进入接口文档地址不需要用户名和密码】
enable: false
# 用户名
username: root
# 密码
password: root
# 是否生产环境【true:接口文档不允许访问。false:接口文档允许访问】
production: false

完整配置文件

参考文档:https://doc.xiaominfo.com/docs/features/enhance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
knife4j:
enable: true
documents:
-
group: 2.X版本
name: 接口签名
locations: classpath:sign/*
setting:
language: zh-CN
enableSwaggerModels: true
enableDocumentManage: true
swaggerModelName: 实体类列表
enableVersion: false
enableReloadCacheParameter: false
enableAfterScript: true
enableFilterMultipartApiMethodType: POST
enableFilterMultipartApis: false
enableRequestCache: true
enableHost: false
enableHostText: 192.168.0.193:8000
enableHomeCustom: true
homeCustomLocation: classpath:markdown/home.md
enableSearch: false
enableFooter: false
enableFooterCustom: true
footerCustomContent: Apache License 2.0 | Copyright 2019-[小飞Knife4j接口文档](https://utils.naste.top/)
enableDynamicParameter: false
enableDebug: true
enableOpenApi: false
enableGroup: true
cors: false
production: false
basic:
enable: false
username: test
password: 12313