You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+React项目PDF流返回超时后出现NaN错误求助

Spring Boot+React PDF流返回超时问题排查与解决

问题描述

我开发了一个Spring Boot后端+React前端的应用,功能是把PDF以流的形式返回给浏览器。但当请求到达Controller后处理时长超过1分钟时,页面会显示错误:Error NaN: undefined Reason: undefined。已经尝试增加前端和后端的超时设置,但问题依然存在,不确定是否和超时相关,需要技术支持。

后端日志

2023-03-06 12:22:33.678 DEBUG 13380 --- [-nio-443-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/print/error?error=undefined&message=undefined&reason=undefined", parameters={masked}
2023-03-06 12:22:33.680 DEBUG 13380 --- [-nio-443-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.jcrew.labelprintbackend.controllers.FrontendController#print()
2023-03-06 12:22:33.682 DEBUG 13380 --- [-nio-443-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.7, */*;q=0.8]
2023-03-06 12:22:33.682 DEBUG 13380 --- [-nio-443-exec-2] o.s.w.servlet.view.InternalResourceView  : View name 'forward:', model {}
2023-03-06 12:22:33.682 DEBUG 13380 --- [-nio-443-exec-2] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/index.html]
2023-03-06 12:22:33.682 DEBUG 13380 --- [-nio-443-exec-2] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index.html?error=undefined&message=undefined&reason=undefined", parameters={masked}
2023-03-06 12:22:33.684 DEBUG 13380 --- [-nio-443-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2023-03-06 12:22:33.687 DEBUG 13380 --- [-nio-443-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 200
2023-03-06 12:22:33.687 DEBUG 13380 --- [-nio-443-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-03-06 12:22:33.723 DEBUG 13380 --- [-nio-443-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/static/css/2.eefd091c.chunk.css", parameters={}
2023-03-06 12:22:33.726 DEBUG 13380 --- [-nio-443-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2023-03-06 12:22:33.766 DEBUG 13380 --- [-nio-443-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/static/js/2.7df57dd0.chunk.js", parameters={}
2023-03-06 12:22:33.767 DEBUG 13380 --- [-nio-443-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2023-03-06 12:22:33.768 DEBUG 13380 --- [-nio-443-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-03-06 12:22:33.774 DEBUG 13380 --- [nio-443-exec-10] o.s.web.servlet.DispatcherServlet        : GET "/static/js/main.2f79f0d9.chunk.js", parameters={}
2023-03-06 12:22:33.776 DEBUG 13380 --- [nio-443-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2023-03-06 12:22:33.777 DEBUG 13380 --- [-nio-443-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/static/css/main.bc6778fd.chunk.css", parameters={}
2023-03-06 12:22:33.778 DEBUG 13380 --- [-nio-443-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2023-03-06 12:22:33.805 DEBUG 13380 --- [nio-443-exec-10] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-03-06 12:22:33.832 DEBUG 13380 --- [-nio-443-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-03-06 12:22:33.859 DEBUG 13380 --- [-nio-443-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
2023-03-06 12:22:33.927 DEBUG 13380 --- [-nio-443-exec-9] o.s.web.servlet.DispatcherServlet        : GET "/favicon.ico", parameters={}

排查方向与解决方案

1. 检查中间件/负载均衡超时

如果应用部署在反向代理(如Nginx、Apache)或负载均衡器后面,这些组件默认可能有60秒的超时设置,即使前后端自己设了更长时间,中间件会先断开连接。

  • Nginx:修改proxy_connect_timeout、proxy_send_timeout、proxy_read_timeout参数,设置为大于处理时长的值(如300s)
  • Apache:调整Timeout、ProxyTimeout参数

2. Spring Boot端深层超时配置

除了常规的Controller请求超时,还要检查以下配置:

  • 异步请求超时:如果用了@Async处理PDF生成,需要配置spring.task.execution.timeout=300s
  • Tomcat连接器超时:在application.properties中设置:
    server.tomcat.connection-timeout=300000
    server.tomcat.keep-alive-timeout=300000
    

3. React端请求处理优化

前端的错误提示NaN: undefined通常是请求中断后错误处理逻辑不严谨导致的,需要:

  • 确保Axios/Fetch的超时设置正确(以Axios为例):
    axios({
      method: 'get',
      url: '/api/pdf',
      responseType: 'blob',
      timeout: 300000 // 5分钟
    }).then(response => {
      // 处理PDF流
    }).catch(error => {
      // 完善错误处理,打印完整error对象,排查具体错误原因
      console.error('请求失败:', error);
    });
    
  • 检查浏览器本身的超时限制:通过浏览器开发者工具的Network面板查看请求状态(是canceled还是timeout)

4. 长任务异步化优化

PDF生成超过1分钟属于长任务,建议改为异步处理:

  • 后端提供触发PDF生成的接口,返回任务ID
  • 前端轮询任务状态接口,直到任务完成
  • 任务完成后,前端再请求PDF流

5. 日志排查补充

当前日志只显示了错误页面的跳转,没有原始PDF请求的处理日志,需要添加:

  • 在PDF生成的Controller方法中添加日志,记录请求开始、结束时间,以及是否抛出异常
  • 查看Tomcat访问日志,确认原始请求的状态码(如504超时、499客户端断开)

内容的提问来源于stack exchange,提问作者Sarthak Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 05:48:11