【问题标题】:Springdoc: got 404 when open swagger-ui.htmlSpringdoc:打开 swagger-ui.html 时得到 404
【发布时间】:2020-10-06 13:19:54
【问题描述】:

我有最新的 Spring Boot 应用程序和 springdoc.swagger-ui。

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.2.32</version>
</dependency>

我的 application.properties 包含 springdoc.swagger-ui.path=/swagger-ui-openapi.html

当我通过 Intellij IDEA 运行应用程序时,http://localhost:8080/swagger-ui-openapi.html 将我带到http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config Swagger UI 页面加载成功。

但是,如果我通过命令行启动应用程序:“java -jar my-app.jar”,我在浏览器中得到 404,并且在尝试访问 http://localhost:8080/swagger-ui-openapi.html 时出现日志“圆形视图路径 [错误]”中的错误 它会将我重定向到http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

javax.servlet.ServletException: Circular view path [error]: would dispatch back to the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

但是,http://localhost:8080/v3/api-docs 是可访问的,并且架构在此地址可用。

我该如何解决这个问题?

【问题讨论】:

  • 你需要swagger-annotations依赖吗?删除后运行您的应用程序。可能导致依赖冲突
  • @Suraj 你好!尝试依赖和不依赖 - 结果相同。此外,我想如果是依赖冲突,从 IDEA 运行时也会重现该问题,但事实并非如此。
  • 我尝试了一个使用这两个依赖项的示例应用程序,它工作正常。看起来像其他问题

标签: spring-boot swagger-ui springdoc springdoc-openapi-ui


【解决方案1】:

对于这个问题,我的结论是: (1) 在IDEA中启动就可以了 (2) 使用 spring-boot-maven-plugin 重新打包 jar 并使用 'java -jar' 启动它也可以。 (3) 如果我尝试以 'java -classpath ".:.conf" DemoApplication' 开头,它不起作用。

所以,对于打包,我使用 spring-boot-maven-plugin。

【讨论】:

    【解决方案2】:

    当您的应用程序在代理、负载平衡器或云中运行时,在我的情况下什么是有效的。

    在您的 Spring Boot 应用程序中,确保您的应用程序处理此标头:X-Forwarded-For。

    有两种方法可以实现:

    在你的属性文件中添加:

    server.use-forward-headers=true
    

    如果这还不够,Spring Framework 提供了一个 ForwardedHeaderFilter。您可以通过将 server.forward-headers-strategy 设置为 FRAMEWORK 将其注册为应用程序中的 Servlet 过滤器。

    自 Spring Boot 2.2 起,这是处理反向代理标头的新属性:

    在你的属性文件中添加

    server.forward-headers-strategy=framework
    

    您可以将以下 bean 添加到您的应用程序中:

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

    如果你的根目录下已经有静态内容,并且你不希望它被 springdoc-openapi-ui 配置覆盖,你可以定义一个自定义配置 swagger-ui,以免覆盖从上下文根中配置文件:

    例如在你的属性文件中使用:

    springdoc.swagger-ui.path= /swagger-ui/api-docs.html
    

    参考: https://springdoc.org/

    【讨论】:

      【解决方案3】:

      springdoc-openapi 不需要 swagger-annotations v1.6.1 依赖;

      默认情况下,使用 springdoc 您不需要任何 ViewResolver 的附加设置。

      你可以看看一些示例代码:

      【讨论】:

      • 我已经删除了 swagger-annotations 并且只留下了 springdoc-openapi-ui 依赖。同样,我没有任何 ViewResolver 的任何附加设置。当我从 IDEA 运行应用程序并转到 localhost:8080/swagger-ui.html - 一切正常。但是如果我将我的应用程序打包到 jar 中并运行它“java -jar my-app.jar” - 我在localhost:8080/swagger-ui.html 得到 404 如果我使用像 springdoc.swagger-ui.path=/mypath/ 这样的自定义路径,也会发生同样的事情swagger-ui.html
      • 顺便说一句...如果我以以下方式启动应用程序:mvn spring-boot:start - 一切正常。问题仅在于直接 java -jar 启动
      • 这似乎是一个错误。我创建了只有 starter 和 springdoc 依赖的新项目,问题仍然存在。我会尝试在 springdoc git 上创建一个问题,让我们看看他们的回答。
      • 这不是错误。有些不同。我尝试构建相同的存储库并启动 jar - 在另一台 PC 上一切正常。我更新了 java,删除了 .m2 并再次下载了依赖项,但没有任何帮助......
      • @DušanSalay 请参阅此部分了解 Wildfly:springdoc.org/#integration-with-wildfly
      猜你喜欢
      • 2020-10-20
      • 2021-02-22
      • 1970-01-01
      • 2021-06-03
      • 2018-06-08
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多