跳到主要内容

【13】 整合knife4j实现网关聚合接口文档

文章目录

    • 前言
  • 方式1 Spring Cloud Gateway
      1. 后台服务配置 knife4j
      1. 网关聚合
  • 方式2 Knife4jAggregation微服务聚合中间件

前言

在微服务架构下,每个后台应用都接入swagger 在线文档,在服务特别多的情况下,就需要做聚合文档处理,也就是将所有服务的文档聚合在一起。

Spring Cloud Gateway作为微服务的API网关,可以整合swagger 实现聚合接口文档。

方式1 Spring Cloud Gateway

方式1使用Spring Cloud Gateway直接聚合,使用knife4j 作为在线接口文档。

1. 后台服务配置 knife4j

首先在后台应用中添加knife4j 依赖。

        <dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>

添加配置类:

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
public class SwaggerConfiguration {



@Bean(value = "userApi")
@Order(value = 1)
public Docket groupRestApi() {


return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(groupApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("org.pearl.app"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo groupApiInfo() {


return new ApiInfoBuilder()
.title("swagger-bootstrap-ui很棒~~~!!!")
.description("<div style='font-size:14px;color:red;'>swagger-bootstrap-ui-demo RESTful APIs</div>")
.termsOfServiceUrl("http://www.group.com/")
.contact("group@qq.com")
.version("1.0")
.build();
}

}