如何使用 springdoc-openapi-webflux-ui 显示应用程序 API 文档?

Sey*_*bol 12 java spring spring-boot springdoc

我阅读了这个https://springdoc.github.io/springdoc-openapi-demos/文档以使用 springdoc-openapi-webflux-ui。正如文档所说,我刚刚springdoc-openapi-webflux-ui在我的应用程序中添加了库:implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.26')

此外,我在 application.yml 中自定义了 API 的路径:

springdoc:
  swagger-ui:
    path: /swagger-ui.html
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序并转到http://localhost:8080/swagger-ui.html 时,它会将我重定向到http://localhost:8080/webjars/swagger-ui/index.html?configUrl=/v3/api -docs/swagger-config。在该页面中,我收到一个错误:

Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Mon Jan 20 05:16:10 UTC 2020
[7192d9dc] There was an unexpected error (type=Not Found, status=404).
No matching handler
Run Code Online (Sandbox Code Playgroud)

问题是:我应该向我的应用程序添加其他配置以显示 API 文档吗?

PS:我使用 spring.boot 2.2.2:RELEASE

小智 3

默认情况下,只需要添加 springdoc-openapi-webflux-ui 的依赖即可。

 <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-webflux-ui</artifactId>
      <version>1.2.32</version>
   </dependency>
Run Code Online (Sandbox Code Playgroud)

您可以看一下演示代码:

您可以检查类路径,并尝试从 IDE 外部运行应用程序。确保您的 IDE 设置正确,具体取决于您的构建工具:

另外,请检查您是否正在使用@EnableWebFlux。

正如 Spring Boot 参考文档中所述,当您使用 @EnableWebFlux 时,您告诉 Spring Boot 您希望完全控制 WebFlux 配置并禁用所有自动配置(包括静态资源):

您有两种解决方案:

  1. 删除@EnableWebFlux
  2. 如果您确实想要控制 Bean 创建并且绝对想要使用 @EnableWebFlux,那么您需要添加 WebFluxConfigurer 的实现。

这已经在这里讨论过:

  • springdoc 演示的链接已损坏 (6认同)