springcloud alibaba集成kenif4j
时间: 2023-12-07 19:38:57 浏览: 89
以下是Spring Cloud Alibaba集成Knife4j的步骤:
1.在pom.xml文件中添加Knife4j和Swagger的依赖:
```xml
<!-- Knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
```
2.在启动类上添加注解@EnableSwagger2和@EnableKnife4j:
```java
@SpringBootApplication
@EnableSwagger2
@EnableKnife4j
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
3.在配置文件中添加Swagger和Knife4j的配置:
```yaml
# Swagger配置
swagger:
enabled: true
title: Demo API
description: Demo API文档
version: 1.0
base-package: com.example.demo.controller
contact:
name: demo
url: https://www.example.com
email: demo@example.com
# Knife4j配置
knife4j:
enabled: true
title: Demo API
description: Demo API文档
version: 1.0
base-package: com.example.demo.controller
contact:
name: demo
url: https://www.example.com
email: demo@example.com
```
4.在Controller类上添加Swagger注解:
```java
@RestController
@Api(tags = "DemoController", description = "Demo接口")
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) {
// ...
}
}
```
5.启动应用程序并访问http://localhost:port/doc.html,即可查看Knife4j生成的API文档。
阅读全文