【发布时间】:2021-09-28 20:45:43
【问题描述】:
将 springdoc-openapi-ui 依赖项添加到我的 Spring 项目(不是 Spring Boot)后生成 OpenAPI V3 文档,可以使用默认的 swagger-ui 页面查看:localhost:8080/swagger-ui.html。因为 springdoc 文档替换了以前的 Swagger 文档,所以我想在同一 URL localhost:8080/docs/index.html 上提供它。根据 springdoc 文档,我得到的印象可以通过使用 application.properties 中的 springdoc.swagger-ui.path 选项来完成:
springdoc.swagger-ui.path=/docs/index.html
但是,我希望能够通过转到 localhost:8080/docs/index.html 导航到 API 文档,但我得到了 404,localhost:8080/swagger-ui.html 仍然有效,但现在重定向到 http://localhost:8080/docs/swagger-ui/index.html?configUrl=/restapi/v3/api-docs/swagger-config。
如何配置我的项目也使 swagger-ui 页面通过自定义 URL 可用,即 localhost:8080/docs/index.html 而不是默认的 localhost:8080/swagger-ui.html?
编辑
在尝试更多使其正常工作并查看在线可用信息后,例如 springdoc FAQ(在 this H3AR7B3A7 的回答中提到)我无法让它工作。我决定采用不同的解决方案,它应该具有相同的效果。 springdoc.swagger-ui.path 选项允许指定自定义 URL,但据我了解,转到自定义 URL 会将用户重定向到标准 localhost:8080/swagger-ui.html 页面。所以现在手动配置重定向:
@RequestMapping("/docs/index.html")
public void apiDocumentation(HttpServletResponse response) throws IOException {
response.sendRedirect("/swagger-ui.html");
}
【问题讨论】:
-
这应该可以,尽管使用 'localhost:8080/docs/index.html' 会将您重定向到 'localhost:8080/docs/swagger-ui/index.html?configUrl=/restapi/v3/…'。您是否设置了其他相关属性?还是您可以共享的配置类?使用带有这些属性的 'localhost:8080/swagger-ui.html' 会给我一个白标签。
-
是的,我的理解是,如果
springdoc.swagger-ui.path选项有效,您将被重定向到http://localhost:8080/docs/swagger-ui/index.html?configUrl=/restapi/v3/api-docs/swagger-config。有一个扩展AuthenticationStatelessContextConfiguration的配置类,它覆盖protected void configure(HttpSecurity http) throws Exception方法并创建一个CorsConfigurationSource corsConfigurationSource()bean。这似乎是唯一可能导致问题的远程配置,但即使这样似乎也很牵强。 -
嗯,这是一种奇怪的行为。这些配置不应该是问题,因为 localhost:8080/swagger-ui.html 有效。我在考虑 SpringDocConfiguration 或 SpringDocConfigProperties bean,因为看起来其他一些配置正在覆盖您的属性,或者由于某种原因您的属性没有被拾取。
-
如果代码不是专有的或以任何方式敏感的,我很乐意看一下......但除此之外,除了分享我的想法之外,我无能为力。 XD
-
可能是配置问题,因为它是 Spring 应用程序而不是 Spring Boot 应用程序,我们没有正确配置 springdoc?基于 springdoc 常见问题解答
org.springdoc.core.SpringDocConfiguration.class和org.springdoc.core.SpringDocConfigProperties.class被添加到上下文中。默认的localhost:8080/swagger-ui.html有效,springdoc.swagger-ui.path选项的作用是配置的值显示在您重定向到的 URL 中,尽管形式与我预期的不同。
标签: spring springdoc springdoc-openapi-ui