api-gateway网关集成swagger2

问题

在SpringCloud项目中多个模块接口都有swagger文档,各自模块的文档地址不一致,前端使用起来十分不方便。在网关模块可以聚合各模块的swagger文档,通过网关地址访问。网上查询了解决方案,但是有些不行或者报以下错误。

Not Found http://localhost:port/FACE-HW/v2/api-docs

仔细观察发现是spring.application.name是大写,手动输入小写的地址是可以正常访问的。然后在配置中将其改为小写,问题完美解决。下面贴出完整代码。

swagger2和lombok依赖


        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
   		</dependency>

配置

SwaggerProvider。

  1. 因为我的网关配置了 lower-case-service-id: true,访问的地址模块实例名要小写,route.getUri().getHost()获取到的模块实例名都是大写,所以访问swagger文档报错。这里可以将获取到的每个实例名转为小,或者设置lower-case-service-id: false,都用大写。
  2. api-gateway和admin模块没有swagger文档,可以在filter中过滤掉。
package com.xhwl.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.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;
/**
 * @author wuhe
 */
@Component
public class SwaggerProvider implements SwaggerResourcesProvider {
    /**
     * swagger2默认的url后缀
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

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


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

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routeHosts = new ArrayList<>();
        // 过滤不必要的模块。host转小写
        routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
                .filter(route -> !"API-GATEWAY".equals(route.getUri().getHost()))
                .filter(route -> !"ADMIN-SERVER".equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost().toLowerCase()));
        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;
    }
}


SwaggerHandler

package com.xhwl.controller;

import com.xhwl.config.SwaggerProvider;
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;

/**
 * @author wuhe
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandler {
    private SwaggerProvider swaggerProvider;

    @Autowired
    public SwaggerHandler(SwaggerProvider swaggerProvider) {
        this.swaggerProvider = swaggerProvider;
    }

    @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<>(swaggerProvider.get(), HttpStatus.OK);
    }
}



你可能感兴趣的:(SpringCloud,spring,boot,java)