【问题标题】:How to integrate Zuul with Swagger如何将 Zuul 与 Swagger 集成
【发布时间】:2020-06-24 14:37:57
【问题描述】:

我在我的微服务项目中遇到了一些生成 swagger 文档的问题。当我添加 zuul 自定义路由时,swagger 文档变得不一致。

例子:

  1. RestController:
    @RestController
    public class Controller {

         @PostMapping("/foo")
         public void foo() {}    
    }
  1. Zuul 路由:
zuul:
  routes:
    foo:
      path: /bar/**
      url: http://localhost:8080/foo
  1. Swagger 配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("my.package"))
                .build();
    }
}
  • 我需要什么:Swagger UI 显示 /bar 端点
  • 我得到的结果:Swagger UI 显示 /foo 端点(这对前端开发人员毫无用处,因为他们在运行时使用 swagger 生成他们的组件)

那么,有什么解决方案可以配置swagger或者zuul来避免这个问题吗?

【问题讨论】:

    标签: spring swagger netflix-zuul


    【解决方案1】:

    我们必须在 application.yml 文件中添加 zuul 配置。

    # custom configuration - used by swagger to rename endpoints in documentation
    springfox:
      documentation:
        swagger:
          v2:
            path: /api/uaa/api-docs
    
    whitelistInternal:
      - method: GET
        path: '/api/**/api-docs'
      - method: GET
        path: '/swagger-ui.html'
      - method: GET
        path: '/swagger-resources/**'
    

    请检查适合您的线路。

    这是一个如何完成的示例 https://dzone.com/articles/centralized-documentation-in-microservice-spring-b

    【讨论】: