带负载平衡器AWS上的OpenAPI Swagger配置

发布于 2025-02-06 19:39:13 字数 2495 浏览 2 评论 0原文

我正在尝试在我的Spring Boot应用程序中添加Swagger(OpenApi),并以以下方式进行配置:

我的 pom.xml 文件具有以下依赖关系:

<!-- Swagger UI -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.9</version>
    </dependency>

应用程序类具有 @springbootapplicationa 注释和以下bean定义:

/** Swagger Configuration */
@Bean
public OpenAPI seatManagerOpenAPI() {
    return new OpenAPI()
            .info(new Info().title("Service API")
                    .description("Some Description")
                    .version("0.0.1")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")))
            .externalDocs(new ExternalDocumentation()
                    .description("Some Documentation")
                    .url("https://somelink")
            );
}

我的Spring Boot应用程序具有一个控制器:

@RestController
@RequestMapping("/serviceName")
public class ServiceNameController

我的 application.yml

springdoc:
  packagesToScan: path.to.service
  pathsToMatch: /serviceName/**
  swagger-ui:
    path: /serviceName/swagger-ui

使用此非常基本的配置,Swagger在我的Localhost上正常工作:

[http:// localhost:8082/serviceName/swagger -ui/index.html]

但是,当部署到AWS上的ECS群集时,我的请求正在通过负载平衡器(ALB) - 我遇到以下错误:“失败:加载远程配置”。在浏览器中检查一下,我在日志中看到了404个状态,并有一条消息:

undefined /v3/api-docs/swagger-config

我有一个在EC上运行的几个微服务的群集。负载平衡器根据路径将各种HTTPS调用指向相关的目标组(服务),例如:

IF Path is /servicename/* AND Host is someHost
Then forward to [relevant target group] (service)

我看到了许多有关类似错误的帖子,并且与API Gateway的配置有关的答案(我根本不使用它在AWS上)或春季安全库(我根本不使用它)。

尝试添加此bean:

@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
    return new ForwardedHeaderFilter();
}

我还在应用程序中使用了许多配置。

springdoc:
#  swagger-ui:
#    use-root-path: false
#    url: "/v3/api-docs"
#    disable-swagger-default-url: true
#    path: /serviceName/swagger-ui
#    configUrl: "/swagger-config.yaml"
#    use-root-path: true
#    urls:
#      url: "/v3/api-docs"
#      name: "seat-manager"
#  api-docs:
#    enabled: true
#    path: /v3/api-docs/swagger-config

我 config.json 文件到我的资源和我的资源/静态文件夹,并来回更改内容,但我无法使其正常工作。

我在这里想念什么?哪种设置实际上会使Swagger在我的云中远程工作?

非常感谢您!

I am trying to add Swagger (OpenAPI) in my Spring Boot Application, and configured it in the following way:

My pom.xml file has the following dependency:

<!-- Swagger UI -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.9</version>
    </dependency>

The application class has a @SpringBootApplication annotation and the following bean defined:

/** Swagger Configuration */
@Bean
public OpenAPI seatManagerOpenAPI() {
    return new OpenAPI()
            .info(new Info().title("Service API")
                    .description("Some Description")
                    .version("0.0.1")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")))
            .externalDocs(new ExternalDocumentation()
                    .description("Some Documentation")
                    .url("https://somelink")
            );
}

My Spring Boot Application has a single controller:

@RestController
@RequestMapping("/serviceName")
public class ServiceNameController

My application.yml has these following additions:

springdoc:
  packagesToScan: path.to.service
  pathsToMatch: /serviceName/**
  swagger-ui:
    path: /serviceName/swagger-ui

With this very basic configuration, Swagger is working perfectly on my localhost:

[http://localhost:8082/servicename/swagger-ui/index.html]

But when deploying to an ECS cluster on AWS, where my request is passing through a load balancer (ALB) - I get the following error: "Failed to load remote configuration". Inspecting this in the browser, I see a 404 status in the logs and a message:

undefined /v3/api-docs/swagger-config

I have a cluster of several micro-services running on ECS. The load balancer directs the various https calls to the relevant target group (service) based on the path, such as:

IF Path is /servicename/* AND Host is someHost
Then forward to [relevant target group] (service)

I have seen many posts about similar errors, and with answers related to configuration of API gateway (which I am not using at all on AWS) or Spring Security libraries (which I am not using at all).

I tried adding this bean:

@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
    return new ForwardedHeaderFilter();
}

I also played around with many configurations in my application.yml file, adding and removing many of these properties with various values (not just the ones included down here):

springdoc:
#  swagger-ui:
#    use-root-path: false
#    url: "/v3/api-docs"
#    disable-swagger-default-url: true
#    path: /serviceName/swagger-ui
#    configUrl: "/swagger-config.yaml"
#    use-root-path: true
#    urls:
#      url: "/v3/api-docs"
#      name: "seat-manager"
#  api-docs:
#    enabled: true
#    path: /v3/api-docs/swagger-config

I also tried adding a swagger-config.json file to my resources and to my resources/static folder, and changed the contents back and forth but I couldn't make it work.

What am I missing here? What setting will actually make swagger work remotely in my cloud?

Thank you so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2025-02-13 19:39:13

我遇到了同样的问题,解决方案是直接的。在ALB上,您还需要揭示Docu JSON页面,然后还需要播放API路径(用于呼叫)。

要简要介绍:要使Swagger在ALB上工作以下需要暴露:

  1. /Swagger -ui* - &gt;访问Swagger Page
  2. /v3 /api -docs* - &gt;需要以使Swagger Page加载
  3. /API* - &gt;需要以使API调用工作

I faced the same problem and the solution is straight forward. On the ALB you need to expose also the docu json page and later also the api path (for the calls).

To shorted things up: To make swagger work over an ALB the following needs to be exposed:

  1. /swagger-ui* -> to access the swagger page
  2. /v3/api-docs* -> needed so that the swagger page loads
  3. /api* -> needed so that the API calls work
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文