斯普林多克:打开大摇大摆的 ui.html 时得到 404



我得到了最新的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>

My 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 可以访问,并且架构在此地址可用。

我该如何解决这个问题?

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

在 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/

对于这个问题,我的结论是: (1(在IDEA中启动它很好 (2(用spring-boot-maven-plugin重新打包jar并以"java -jar"开始也可以。 (3(如果我尝试从诸如"java -classpath".:.conf"演示应用程序"开始,则不起作用。

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

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

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

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

  • https://github.com/springdoc/springdoc-openapi-demos

最新更新