一、异常分析:
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.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at com.zw.baseutil.auth.FeignConfig.apply(FeignConfig.java:30) ~[baseUtil-baseUtil-8.25-1.jar:baseUtil-8.25-1]
at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:176) ~[feign-core-10.7.4.jar:?]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:101) ~[feign-core-10.7.4.jar:?]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:80) ~[feign-core-10.7.4.jar:?]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.7.4.jar:?]
at com.sun.proxy.$Proxy214.getQuestion(Unknown Source) ~[?:?]
该错误信息在RequestContextHolder这个类中,原因如标题所说,主线程的请求结束后销毁了request,子线程得不到request信息。
二、解决方法:
在主线程里添加以下代码(要在调用异步方法之前添加):
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);
代码注解:将RequestAttributes对象设置为子线程共享
完美解决!!!