Swagger 文档集成

默认Swagger是集成在各个服务中,前端工程师进行接口对接或者测试工程师进行接口测试,需要进入各个服务进行操作,十分不便;这里我编写了一个插件,实现在网关把各个服务的网关接口信息汇总展示。

1、修改pom.xml

添加如下配置:

<properties>
    <!-- http://oss.jfrog.org/artifactory/oss-snapshot-local/io/springfox/ -->
    <springfox.version>3.0.0-SNAPSHOT</springfox.version>
    <swagger.version>1.5.22</swagger.version>
    <swagger-bootstrap-ui.version>1.9.4</swagger-bootstrap-ui.version>
    <swagger-butler.version>1.2.0</swagger-butler.version>
</properties>

<dependencyManagement>
    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-gateway-dependencies -->
        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway-dependencies</artifactId>
            <version>2.1.1.RELEASE</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency> -->
        <dependency>
            <groupId>com.github.vindell</groupId>
            <artifactId>spring-cloud-gateway-swagger2</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- For Swagger -->
    <dependency>
        <groupId>com.github.vindell</groupId>
        <artifactId>spring-cloud-gateway-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
    </dependency>
</dependencies>

2、以 application-local.yaml 为例

################################################################################################################
###Spring Boot 相关组件(SpringMVC、Freemarker、Session、Cache、DataSource)配置:
################################################################################################################
spring:
  cloud:
    gateway:
      enabled: true
      swagger:
        enabled: true
        authorization:
          key-name: X-Authorization
        title: 微服务开发平台 - 服务网关
        description: 该模块完成各模块的接口中转
        version: ${application.version}
        contact:
          name: Jeebiz
          url: http://jeebiz.net
        base-package: net.jeebiz
        # 公共参数
        global-operation-parameters:
          - name: X-Authorization
            description: JWT鉴权
            modelRef: string
            parameterType: header
            required: true

服务节点配置(需要注册中心已配置)

根据 Spring Gateway 配置要求添加各服务的路由信息:

################################################################################################################
###Spring Boot 相关组件(SpringMVC、Freemarker、Session、Cache、DataSource)配置:  
################################################################################################################
spring:
  cloud:
    gateway:
      enabled: true
      discovery:
        locator:
          enabled: true
      filter-map:
        '[/]': anon
        '[/**/favicon.ico]': anon
        '[/webjars/**]': anon
        '[/assets/**]': anon
        '[/error*]': anon
        '[/logo/**]': anon
        '[/swagger-ui.html**]': anon
        '[/swagger-resources/**]': anon
        '[/doc.html]': anon
        '[/**/bycdao-ui/**]': anon
        '[/**/v2/**]': anon
        '[/kaptcha*]': anon
        '[/actuator*]': anon
        '[/actuator/**]': anon
        '[/dingtalk/**]': anon
        '[/**/authz/login/jwt]': anon
        '[/**/authz/login/dingtalk]': anon
      default-filters:
        #- name: RateLimiter
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback
        - name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
      routes:
        - id: jeebiz-authz
          uri: lb://jeebiz-authz
          predicates:
            - Path=/jeebiz-authz/**
          filters:
            - StripPrefix=1
            #- name: RequestRateLimiter
              #args: 
                #key-resolver: '#{@hostAddrKeyResolver}' 
                #redis-rate-limiter.replenishRate: 10
                #redis-rate-limiter.burstCapacity: 30
        - id: jeebiz-inform
          uri: lb://jeebiz-inform
          predicates:
            - Path=/jeebiz-inform/**
          filters:
            - StripPrefix=1
        - id: jeebiz-settings
          uri: lb://jeebiz-settings
          predicates:
            - Path=/jeebiz-settings/**
          filters:
            - StripPrefix=1
        - id: jeebiz-baseinfo
          uri: lb://jeebiz-baseinfo
          predicates:
            - Path=/jeebiz-baseinfo/**
          filters:
            - StripPrefix=1

配置完成后,启动网关,访问网关地址可见如下界面:

在网关界面切换不同的服务,即可查看不同服务的接口信息。

作者:Jeebiz  创建时间:2019-10-13 15:55
 更新时间:2023-01-09 16:42