关于No thread-bound request found: Are you referring to request attributes outside of an actual web request报错的修改


java.util.concurrent.ExecutionException: 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: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

 

原因在于在异步线程里面调用了

HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
为了避免出错,可以再封装一下
 public RequestAttributes getRequestAttributesSafely(){
  RequestAttributes requestAttributes = null;
try{
   requestAttributes = RequestContextHolder.currentRequestAttributes();
}catch (IllegalStateException e){
  requestAttributes = new NonWebRequestAttributes();
}
  return requestAttributes;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM