springBoot3集成knife4j的全过程,要详细详细的步骤
时间: 2023-05-30 08:06:29 浏览: 113
1. 添加依赖
在`pom.xml`文件中添加以下依赖:
```xml
<!-- Swagger UI -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!-- Knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
```
2. 配置Swagger
在`application.yml`文件中添加以下配置:
```yaml
spring:
profiles:
active: dev
application:
name: demo
swagger:
title: Demo API
description: Demo API Document
version: 1.0
contact:
name: Demo Team
url: https://www.demo.com
email: demo@demo.com
base-package: com.demo.controller
enable: true
host: localhost:8080
```
3. 配置Knife4j
在`application.yml`文件中添加以下配置:
```yaml
knife4j:
swagger-ui:
title: Demo API
description: Demo API Document
version: 1.0
contact:
name: Demo Team
url: https://www.demo.com
email: demo@demo.com
enable: true
host: localhost:8080
```
4. 编写Controller
```java
@RestController
@Api(tags = "Demo API")
public class DemoController {
@ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) {
User user = new User();
user.setId(id);
user.setName("Demo");
user.setAge(18);
return user;
}
}
```
5. 启动应用
启动应用后,在浏览器中访问`http://localhost:8080/doc.html`即可看到Swagger UI界面。
阅读全文