项目分为UserService、ShopService和OrderService,现在需要将其API接口文档进行整合。文章源自懂站帝-http://www.sfdkj.com/12811.html
目的:将ShopService和OrderService中的API接口文档整合到UserService项目中。文章源自懂站帝-http://www.sfdkj.com/12811.html
核心:采用cloud模式聚合文章源自懂站帝-http://www.sfdkj.com/12811.html
技术架构文章源自懂站帝-http://www.sfdkj.com/12811.html
- SpringBoot
- Knife4j
这里仅介绍Cloud模式聚合的代码演示,对于Knife4j的学习,请移步:knife4j。文章源自懂站帝-http://www.sfdkj.com/12811.html
基础信息
UserService项目
核心依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
API接口文档配置类
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
@Bean("user-service")
public Docket userService() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("01.UserService")
.select()
.apis(RequestHandlerSelectors.basePackage("com.ijunfu.user.controller"))
.paths(PathSelectors.any())
.build();
}
}
application.yml
server:
port: 8080
ShopService项目
核心依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
API接口文档配置类
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
@Bean("shop-service")
public Docket userService() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("02.ShopService")
.select()
.apis(RequestHandlerSelectors.basePackage("com.ijunfu.shop.controller"))
.paths(PathSelectors.any())
.build();
}
}
application.yml
server:
port: 8081
OrderService项目
核心依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
API接口文档配置类
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
@Bean("order-service")
public Docket userService() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("03.OrderService")
.select()
.apis(RequestHandlerSelectors.basePackage("com.ijunfu.order.controller"))
.paths(PathSelectors.any())
.build();
}
}
application.yml
server:
port: 8082
重点来了
UserService
引入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-aggregation-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
添加配置
knife4j:
enableAggregation: true
cloud:
enable: true
routes:
- name: 用户服务
uri: http://localhost:8080
location: /v2/api-docs?group=01.UserService
- name: 店铺&商户服务
uri: http://localhost:8081
location: /v2/api-docs?group=02.ShopService
- name: 订单服务
uri: http://localhost:8082
location: /v2/api-docs?group=03.OrderService
打完收工,赶紧启动你的UserService项目文章源自懂站帝-http://www.sfdkj.com/12811.html