SpringCloudGateway集成Swagger3

  1. 后台服务Swagger配置
  1. 网关配置
package com.synda.seckill.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
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.HashSet;
import java.util.List;
import java.util.Set;
@Primary
@Component
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
          
   

    /**
     * swagger2默认的url后缀,此处,不管你是2还是3版本的swagger,都是v2,如果使用v3,调用接口时,服务名前缀就会丢失,别问为什么,问就是通了一个宵
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

    /**
     * 网关路由
     */
    private final RouteLocator routeLocator;

    /**
     * 网关应用名称
     */
    @Value("${spring.application.name}")
    private String self;

    @Autowired
    public MySwaggerResourceProvider(RouteLocator routeLocator) {
          
   
        this.routeLocator = routeLocator;
    }

    @Override
    public List<SwaggerResource> get() {
          
   
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routeHosts = new ArrayList<>();
        // 获取所有可用的host:serviceId
        routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
                .filter(route -> !self.equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost()));

        // 记录已经添加过的server,存在同一个应用注册了多个服务在eureka上
        Set<String> dealed = new HashSet<>();
        routeHosts.forEach(instance -> {
          
   
            // 拼接url
            String url = "/" + instance + SWAGGER2URL;
            if (!dealed.contains(url)) {
          
   
                dealed.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(instance);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}
package com.synda.seckill.gateway.controller;


import com.synda.seckill.gateway.config.MySwaggerResourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.*;

import java.util.List;

/**
 * swagger聚合接口,三个接口都是swagger-ui.html需要访问的接口
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
          
   

    private MySwaggerResourceProvider mySwaggerResourceProvider;

    @Autowired
    public SwaggerResourceController(MySwaggerResourceProvider mySwaggerResourceProvider) {
          
   
        this.mySwaggerResourceProvider = mySwaggerResourceProvider;
    }

    @RequestMapping(value = "/configuration/security")
    public ResponseEntity<SecurityConfiguration> securityConfiguration() {
          
   
        return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping(value = "/configuration/ui")
    public ResponseEntity<UiConfiguration> uiConfiguration() {
          
   
        return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping
    public ResponseEntity<List<SwaggerResource>> swaggerResources() {
          
   
        return new ResponseEntity<>(mySwaggerResourceProvider.get(), HttpStatus.OK);
    }
}
经验分享 程序员 微信小程序 职场和发展