SpringCLoud Gateway 与 Swagger聚合

版本:

SpringBoot:2.0.6

SpringBloud:Finchley.SR2

Swagger:2.9.2

1、所需加入swagger的模块加入swagger依赖

    2.9.2
    2.9.2

      
      
        io.springfox
        springfox-swagger2
        ${springfox-swagger2.version}
      
      
        io.springfox
        springfox-swagger-ui
        ${springfox-swagger-ui.version}
      

2、在springcloud中新建一下几个类

package com.gitee.cashzhang27.ftf.gateway.configuration;

import com.gitee.cashzhang27.ftf.gateway.properties.SwaggerIgnoreClientProperties;
import com.gitee.cashzhang27.ftf.gateway.route.RedisRouteDefinitionRepository;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteDefinition;
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;

/**
 * 聚合接口文档注册
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/03/11 21:26
 */
@Component
@Primary
public class RedisRouterSwaggerResourcesProvider implements SwaggerResourcesProvider {

  public static final String API_URI = "/v2/api-docs";
  @Value("${spring.profiles.active}")
  private String active;
  @Resource
  private RedisRouteDefinitionRepository routeDefinitionRepository;
  @Resource
  private SwaggerIgnoreClientProperties swaggerIgnoreClientProperties;

  @Override
  public List get() {
    List resources = new ArrayList<>();
    List routes = new ArrayList<>();
    List ignoreClientIds = swaggerIgnoreClientProperties.getClientIds();
    routeDefinitionRepository.getRouteDefinitions().subscribe(routes::add);
    routes.forEach(routeDefinition -> {
      if (!ignoreClientIds.contains(routeDefinition.getId())) {
        routeDefinition.getPredicates().stream()
            .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName()))
            .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
                predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
                    .replace("/**", API_URI).replace("{api}", "api-" + active)))

            );
      }
    });

    return resources.stream().sorted(Comparator.comparing(SwaggerResource::getName))
        .collect(Collectors.toList());
  }

  private SwaggerResource swaggerResource(String name, String location) {
    SwaggerResource swaggerResource = new SwaggerResource();
    swaggerResource.setName(name);
    swaggerResource.setLocation(location);
    swaggerResource.setSwaggerVersion("2.0");
    return swaggerResource;
  }

}
package com.gitee.cashzhang27.ftf.gateway.handler;

import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/06/09 11:04
 */
@Slf4j
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandler {

  @Autowired(required = false)
  private SecurityConfiguration securityConfiguration;
  @Autowired(required = false)
  private UiConfiguration uiConfiguration;
  private final SwaggerResourcesProvider swaggerResources;

  @Autowired
  public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
    this.swaggerResources = swaggerResources;
  }

  @GetMapping("/configuration/security")
  public Mono> securityConfiguration() {
    return Mono.just(new ResponseEntity<>(
        Optional.ofNullable(securityConfiguration)
            .orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
  }

  @GetMapping("/configuration/ui")
  public Mono> uiConfiguration() {
    return Mono.just(new ResponseEntity<>(
        Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()),
        HttpStatus.OK));
  }

  @GetMapping("")
  public Mono swaggerResources() {
    return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
  }
}
package com.gitee.cashzhang27.ftf.gateway.properties;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 不需要加入到 Swagger 聚合的路由配置
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/06/09 21:18
 */
@Data
@Component
@RefreshScope
@Configuration
@ConditionalOnExpression("!'${swagger.ignore.client.client-Ids}'.isEmpty()")
@ConfigurationProperties(prefix = "swagger.ignore.client")
public class SwaggerIgnoreClientProperties {

  private List clientIds = new ArrayList<>();
}
swagger:
  ignore:
    client:
      # 不需要加入到聚合的路由
      client-ids:
        - ftf-auth

 

你可能感兴趣的:(SpringCloud)