Gateway+Swagger2配置聚合文档

Spring Cloud Gateway网关模块聚合各微服务的Swagger接口文档效果如下图:相关pom依赖 <!-- 网关路由代理 (仅网关)--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</arti
摘要由CSDN通过智能技术生成

Spring Cloud Gateway网关模块聚合各微服务的Swagger接口文档

效果如下图:在这里插入图片描述
相关pom依赖

        <!-- 网关路由代理 (仅网关)-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <!--swagger2 (网关和服务端)-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>

Gateway配置
因为Swagger暂不支持webflux项目,所以Gateway里不能配置SwaggerConfig

import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: 采用路由id做资源名称,predicates.Path做映射的方式.
 * 如果需要自定义资源名称,可以单独配置一个集合(route.id,name),创建swaggerResource时,
 * 通过route.id获取name.
 * <p>例如:     swagger:
 *               route_names:
 *                 - name: 订单
 *                   id: order_route
 *                 - name: 订单1
 *                   id: order_route1
 *              spring: cloud: gateway: routes:
 *                                         - id: order_route
 *                                         - id: order_route1
 *  </p>
 * 重新Gateway的方法还没研究出来 - -||
 *
 * @Author:
 * @Date: 2021-04-25 15:38
 * @Version 1.0
 **/
@Component
@Primary
public class Swagger2 implements SwaggerResourcesProvider {
   

    protected static final String API_URI = "/v2/api-docs";//固定后缀
    private final RouteLocator routeLocator;
    private final GatewayProperties gatewayProperties;
    //资源集合
    private static List<SwaggerResource> resources;
	//自定义资源名称需要在这里初始化
    public Swagger2(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
   
        this.routeLocator = routeLocator;
        this.gatewayProperties = gatewayProperties;
    }

    @Override
    public List<SwaggerResource> get() {
   
    //做了一个简单缓存,没仔细设计,其实可有可无用处不大
        if (resources == null) {
   
            resources = new ArrayList<>();

            List<String> routes = new ArrayList<>();
            routeLocator.getRoutes().subscribe(route 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值