关于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;
}
posted @ 2020-12-18 14:19  威少wang  阅读(4499)  评论(0编辑  收藏  举报