首页 > 解决方案 > 当我包含大型资源文件并构建/运行 JAR 文件时,为什么我的 Springboot Springdoc /swagger-ui.html 端点返回 404?

问题描述

当存在大型资源文件(例如,每个 ~380Mb 的 7 个文件)时,为什么 SpringDoc swagger-ui.html 返回 404 / Whitelabel 错误页面?这是在构建和运行 JAR 文件之后发生的。当项目直接从 Intellij 运行时,端点工作正常。

我已经能够使用 SpringDoc 演示存储库复制该错误。访问时的预期行为/swagger-ui.html是让 swagger-ui 页面加载显示图书服务资源。但是,当存在大型资源文件时,我得到的是 404,日志显示:

19:30:49.012 [http-nio-8080-exec-2] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 14 ms
19:30:49.023 [http-nio-8080-exec-2] TRACE o.s.web.servlet.DispatcherServlet - GET "/swagger-ui.html", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
19:30:49.026 [http-nio-8080-exec-2] TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc#redirectToUi(HttpServletRequest)
19:30:49.036 [http-nio-8080-exec-2] TRACE o.s.web.method.HandlerMethod - Arguments: [org.apache.catalina.connector.RequestFacade@5e68b13]
19:30:49.064 [http-nio-8080-exec-2] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
19:30:49.064 [http-nio-8080-exec-2] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Nothing to write: null body
19:30:49.065 [http-nio-8080-exec-2] TRACE o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Applying default cacheSeconds=-1
19:30:49.065 [http-nio-8080-exec-2] TRACE o.s.web.servlet.DispatcherServlet - No view rendering, null ModelAndView returned.
19:30:49.065 [http-nio-8080-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Completed 302 FOUND, headers={masked}
19:30:49.071 [http-nio-8080-exec-3] TRACE o.s.web.servlet.DispatcherServlet - GET "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
19:30:49.073 [http-nio-8080-exec-3] TRACE o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns [/swagger-ui*/**, /**]
19:30:49.073 [http-nio-8080-exec-3] TRACE o.s.w.s.h.SimpleUrlHandlerMapping - Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler [Classpath [META-INF/resources/webjars/]]] and 3 interceptors
19:30:49.074 [http-nio-8080-exec-3] DEBUG o.s.w.s.r.ResourceHttpRequestHandler - Resource not found
19:30:49.074 [http-nio-8080-exec-3] TRACE o.s.web.servlet.DispatcherServlet - No view rendering, null ModelAndView returned.
19:30:49.074 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND, headers={masked}

重现

  1. 克隆https://github.com/springdoc/springdoc-openapi-demos
  2. 创建7个大型资源文件: head -c 400000000 /dev/urandom > BigTestFile1.bin head -c 400000000 /dev/urandom > BigTestFile2.bin head -c 400000000 /dev/urandom > BigTestFile3.bin head -c 400000000 /dev/urandom > BigTestFile4.bin head -c 400000000 /dev/urandom > BigTestFile5.bin head -c 400000000 /dev/urandom > BigTestFile6.bin head -c 400000000 /dev/urandom > BigTestFile7.bin
  3. 创建一个名为LargeFilesspringdoc-openapi-book-service src/main/java/resources 的文件夹,并将所有 7 个 TestFiles 放在该文件夹中。
  4. 构建罐子mvn clean package
  5. 运行罐子java -jar ./target/springdoc-openapi-book-service-3.1.6-SNAPSHOT.jar
  6. 导航到 http://0.0.0.0:8080/swagger-ui.html。

我一直在研究许多 API——这个问题似乎只在存在大型资源文件时才会出现,并且仅在构建/运行 JAR 文件之后才会出现。

标签: javaspring-bootspringdocspringdoc-openuispringdoc-ui

解决方案


推荐阅读