解决No thread-bound request found: Are you referring to request attributes outside of an actual web...
2020-05-11 11:19:46
异常信息:
java.lang.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.
错误原因大致是因为主线程完成后,子线程不能获取request请求信息,包括session等导致此错误。
在网上找的一片文章参考,只要在开启新线程前添加如下,让子线程共享主线程servletRequestAttributes即可,亲测可用。
具体可参考原文:
原文链接点击
ServletRequestAttributes servletRequestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(servletRequestAttributes,true);//设置子线程共享
分布式项目中,我们会把一些服务提取出来形成一个服务提供者,并且单独发布为一个项目。一般发布成一个jar包,这个服务提供者可以称之为 soa项目。
soa只是一个简单的接口项目,不是web...
场景描述:A服务利用RPC 调用B 服务, B 服务里请求了一个C服务的接口, 但是C服务需要接口授权,利用 HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();解决: 换一中授权方式,直接生成了一个授权token, 不用RequestContextHolder 这种方式获取请求token。
,此时就出现一个问题,当采用web端调用时(前端登录系统发请求),能够正常将返回的数据插入数据库,但是采用定时任务(schedule-----@EnableScheduling)调用接口时,数据无法正常插入数据库,出现报错。在进行Spring Boot项目开发时,项目中出现一个匪夷所思的问题:项目中的一个接口,在调用其他接口完成逻辑处理后,需要将该接口返回的数据存储在数据库中(接口,考虑到可能是框架存在的问题,就自己写了个插入数据库的sql。写完成通过定时任务调用,发现数据成功插入了。
问题描述:
xxljob定时任务中写了一个线程池,每次调用定时任务,都会起7条线程,进行数据传输,程序运行时报错:java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attribu
bug原因:
线程池中的子线程获取不到定时任务主线程的request信息
解决方法:
在定义线程池之前加入两行代码,设置request子线程共享
// 子线程request共享
ServletRe
通过webservice接口调用程序时,发现在执行查询的时候一直报一个错误,错误信息如下:
java.lang.IllegalStateExceptino:No thread-bound request found:are you referring to request attributes outside of an actual web request,or processi...
使用背景:今天在spring-cloud项目中,使用多线程异步调用微服务出现的错误
Nothread-boundrequestfound:Areyoureferringtorequestattributesoutsideofanactualwebrequest,orprocessingarequestoutsideoftheoriginallyr...
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?
java代码报错java.lang.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
Caused by: java.lang.IllegalStateException: No thread-bound request found:
Are you referring to request attributes outside of an actual web request,
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
CompletableFuture.supplyAsync(() -> {
RequestContextHolder.setRequestAttributes(servletRequestAttrib.