gpt4 book ai didi

spring - 如何将 Zuul 与 Swagger 集成

转载 作者:行者123 更新时间:2023-12-04 13:38:05 25 4
gpt4 key购买 nike

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

例子:

  • 休息 Controller :

  •     @RestController
    public class Controller {

    @PostMapping("/foo")
    public void foo() {}
    }

  • Zuul 路由:

  • zuul:
    routes:
    foo:
    path: /bar/**
    url: http://localhost:8080/foo

  • 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 来避免该问题吗?

    最佳答案

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

    # 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

    关于spring - 如何将 Zuul 与 Swagger 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60655896/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com