sprint-boot 怎么使用 knife4j
时间: 2023-08-23 07:07:04 浏览: 79
使用Knife4j来生成API接口文档需要以下步骤:
1. 引入Knife4j依赖
在pom.xml中添加以下依赖:
```xml
<!--Knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
```
2. 配置Knife4j
在application.yml中添加以下配置:
```yaml
knife4j:
version: 2.0.2
title: 接口文档
description: 接口文档描述
contact:
name: xxx
url: https://xxx.com
email: xxx@xxx.com
license:
name: Apache License 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
termsOfServiceUrl: https://xxx.com/terms-of-service.html
```
其中,version为Knife4j的版本号,title为文档标题,description为文档描述,contact为联系人信息,license为许可证信息,termsOfServiceUrl为服务条款链接。
3. 编写接口文档注解
在接口方法上添加接口文档注解,例如:
```java
@ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "path")
@GetMapping("/{userId}")
public UserInfo getUserInfo(@PathVariable Long userId) {
//...
}
```
其中,@ApiOperation为方法级别注解,用于描述接口信息,包括value、notes等属性;@ApiImplicitParam为方法参数级别注解,用于描述参数信息,包括name、value、required、dataType等属性。
4. 访问接口文档
启动应用程序后,在浏览器中访问如下地址即可查看生成的API接口文档:
```
http://localhost:port/doc.html
```
其中,port为应用程序启动的端口号。
阅读全文
相关推荐


















