關於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