No thread-bound request found: Are you referring to request attributes outside of an actual web requ
今天收到一个bug,乍一看是个陌生面孔。
报错信息
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
看了一下是在spring-web中RequestContextHolder.currentRequestAttributes方法抛出来的。
/*** Return the RequestAttributes currently bound to the thread.* <p>Exposes the previously bound RequestAttributes instance, if any.* Falls back to the current JSF FacesContext, if any.* @return the RequestAttributes currently bound to the thread* @throws IllegalStateException if no RequestAttributes object* is bound to the current thread* @see #setRequestAttributes* @see ServletRequestAttributes* @see FacesRequestAttributes* @see javax.faces.context.FacesContext#getCurrentInstance()*/public static RequestAttributes currentRequestAttributes() throws IllegalStateException {RequestAttributes attributes = getRequestAttributes();if (attributes == null) {if (jsfPresent) {attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();}if (attributes == null) {throw new IllegalStateException("No thread-bound request found: " +"Are you referring to request attributes outside of an actual web request, " +"or processing a request outside of the originally receiving thread? " +"If you are actually operating within a web request and still receive this message, " +"your code is probably running outside of DispatcherServlet/DispatcherPortlet: " +"In this case, use RequestContextListener or RequestContextFilter to expose the current request.");}}return attributes;}
看了方法注释说,如果当前线程没有绑定请求request的属性,就会报这个错。
问题排查
可是我们有用到HttpServletRequest相关的属性,怎么会报这个错呢?
最终发现,我们私库里的代码,根据国际化获取提示信息的一段。
/*** 获取国际化消息.* * @param code* 代码* @param args* 参数* @return 国际化消息*/public String getMessage(String code, Object... args) {Locale locale = localeResolver.resolveLocale(WebUtil.getRequest());return messageSource.getMessage(code, args, locale);}
其中WebUtil.getRequest()就是获取这个RequestAttributes的方法。
/*** Gets the request.* * @return the request*/public static HttpServletRequest getRequest() {ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();return attr.getRequest();}
发现问题所在了。
问题分析
在之前的方法调用的没有发现这个问题,原因是请求中会包含HttpServletRequest对象,不会报错。而我这次的调用是将流程移到异步线程中执行,中途丢失了HttpServletRequest对象,所以就获取不到,报这个错。
解决方法
本次我是直接调了另外一个方法,跳过了获取Locale的步骤。
/*** 获取国际化消息.* @param code 代码* @param args 参数* @return 国际化消息*/public String getMessageWithDefaultLocale(String code, Object... args) {return messageSource.getMessage(code, args, Locale.SIMPLIFIED_CHINESE);}
另外就需要考虑将HttpServletRequest对象设置到异步线程池的线程中。
No thread-bound request found: Are you referring to request attributes outside of an actual web requ相关推荐
- 解决No thread-bound request found: Are you referring to request attributes outside of an actual web.异常
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder. ...
- setCharacterEncoding 是在request.getParameter获取参数之前 设置request的编码格式 一步到位...
setCharacterEncoding 是在request.getParameter获取参数之前 设置request的编码格式 一步到位 转载于:https://www.cnblogs.com/cl ...
- 总结:request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...
- Java中request有哪些方法,Java--获取request中所有参数的方法
java获取request中的参数.java解析URL问号后的参数.有时候我们需要从request中获取参数,或者获取拼接在Url后面的参数,有时候一个一个去拿有点麻烦,一起拿出来放在一个map里面需 ...
- request对象的相关方法和获取request对象的方式
request对象相关方法如下: 1.获取客户机环境信息常见方法:: 1.getRequestURL方法返回客户端发出请求时的完整URL.2.getRequestURI方法返回请求行中的资源名部分.3 ...
- php怎么取request,PHP-如何在Guzzle中获取Request对象?
我需要使用Guzzle检查数据库中的很多项目.例如,项目数量为2000-5000.将其全部加载到单个数组中太多了,因此我想将其分成多个块:SELECT * FROM items LIMIT100.当最 ...
- python urllib3 request 无返回结果_python urllib request urlopen请求网页返回bytes类型
首次调用read()时有返回,之后的调用一直返回b'' >>> f = request.urlopen('http://www.baidu.com/') >>> f ...
- python request headers获取_Python爬虫实战—— Request对象之header伪装策略
在header当中,我们经常会添加两个参数--cookie 和 User-Agent,来模拟浏览器登录,以此提高绕过后台服务器反爬策略的可能性. User-Agent获取 User-Agent可通过随 ...
- 413 request entity too large php,413 Request Entity Too Large
昨天用wordpress发布文章,文章内容是一个网站一段时间来的运行信息,20几页的列表结果粘到编辑器. 点击发布后,页面卡住了,过了一会返回浏览器显示"413 Request Entity ...
- python request url编码_Python爬虫进阶——Request对象之Get请求与URL编码【英雄联盟吧】...
在上一篇中,我们是通过urllib.request.urlopen直接访问的网页地址,但在实际应用中,我们更多地使用urllib.request.Request对象,因为其可以封装headers和da ...
最新文章
- ABAP和Java里关于DEFAULT(默认)机制的一些语言特性
- HDU-2444 The Accomodation of Students
- JS转换HTML转义符
- [导入]php 安全基础 附录C. 加密
- 【收藏】SonarQube-插件-离线安装PMD+阿里P3C
- gj6 深入python的set和dict
- 含代码 | 支付宝如何优化移动端深度学习引擎?
- 日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用
- Spring Boot 项目瘦身指南,非常实用!
- 用StyleCop规范团队代码
- Entity Framework 4.1 : 贪婪加载和延迟加载
- 第二篇 模拟电子技术基础
- git(5)---Git 与Repo 管理Android代码
- Spring @Import
- Atiti 高并发程序设计 艾提拉著 目录 1. 第—部分 基础知识	2 1.1. 第1章 并行计算机的硬件基础 1.1 并行计算机的组成 1.2 共享存储器多处理器系统 1.3 消
- Python调用科大讯飞语音合成离线SDK
- 卸载掉360之后无法删除360safe文件夹解决办法!
- Python抖音视频去水印,并打包成exe可执行文件
- win10 wifi连接不上服务器未响应,Win10连不上WiFi怎么办?Win10连不上WiFi解决方法介绍...
- python docx文档内容提取与写入(汇总)
热门文章
- 阿里巴巴JAVA代码规范一【Block】
- vue接口总是请求超时_vue axios调用接口时请求超时
- 挂载NFS的两种方法
- python文本字词分割及词库云
- 【程序人生】有个程序员男朋友是什么体验?被公开吐槽
- h5支付不能打开支付宝 ios_iOS解决H5支付跳转到支付App及返回原App问题
- Proximal Policy Optimization(PPO)算法原理及实现!
- 产品原型设计规范——Axure
- 噪声dba是什么单位_请问:dBA是什么意思?和dB,dBm之类有什么关系?主要用... - 射频微波研发讨论区 - 52RD研发论坛...
- EBS 使用API更新物料属性 Inv_Item_Grp