Swagger
使用
Swagger
只需按照它的规范去定义接口以及接口相关信息,就可以做到生成接口文档,以及在线接口调试页面
预览效果
Knife4j
是为Java Mvc 框架集成Swagger生成Api文档的增强解决方案。
配置
导入knife4j的maven坐标
1
2
3
4
5<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>配置类
WebMvcConfiguration
中加入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/**
* 配置类,注册web层相关组件
*/
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
/**
* 通过knife4j生成接口文档
* @return
*/
public Docket docket() {
log.info("开始生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("XXXX项目接口文档")
.version("2.0")//版本
.description("XXXX项目接口文档")//简介
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
//指定生成接口需要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))//
.paths(PathSelectors.any())
.build();
return docket;
}
}当接口功能差不多,需要区分用户端接口和管理端接口时,只需添加关键词
groupName("用户端接口")
,多写一份docket即可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
public Docket docket1() {
log.info("开始生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("苍穹外卖项目接口文档")
.version("2.0")
.description("苍穹外卖项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("用户端接口")
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.sky.controller.user"))
.paths(PathSelectors.any())
.build();
return docket;
}
public Docket docket2() {
log.info("开始生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("苍穹外卖项目接口文档")
.version("2.0")
.description("苍穹外卖项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("管理端接口")
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin"))
.paths(PathSelectors.any())
.build();
return docket;
}在配置类
WebMvcConfiguration
设置静态资源映射,否则接口文档页面无法访问1
2
3
4
5
6
7
8
9/**
* 设置静态资源映射
* @param registry
*/
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始设置静态资源映射...");
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
启动项目,可以看到控制台输出日志:开始生成接口文档…和开始设置静态资源映射…,访问localhost:8080/doc.html
即可看到
常用注解
通过注解可以控制生成的接口文档,使接口文档拥有更好的可读性,常用注解如下:
注解 | 说明 |
---|---|
@Api | 用在类上,例如Controller,表示对类的说明,例:@Api(tags="员工相关接口") |
@ApiModel | 用在类上,例如entity、DTO、VO,例:@ApiModel(description = "员工登录时传递的数据模型") |
@ApiModelProperty | 用在属性上,描述属性信息,例:@ApiModelProperty("用户名") |
@ApiOperation | 用在方法上,例如Controller的方法,说明方法的用途、作用,例:@ApiOperation(value="员工登录") |
注解应用示例
@Api
1 |
|
@ApiOperation
1 |
|
@ApiModel,@ApiModelProperty
1 |
|
SpringBoot3使用Swagger3
引入依赖和阿里云镜像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<dependencies>
<!--swagger3-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<repositories>
<!--阿里云镜像-->
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>启动项目访问:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 XuSir'Blog!
评论