報錯信息如下
"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."
這個原因:查看源碼
attributes == null 了
public static RequestAttributes currentRequestAttributes() throws IllegalStateException { RequestAttributes attributes = getRequestAttributes(); if (attributes == null) { if (jsfPresent) { attributes = FacesRequestAttributesFactory.getFacesRequestAttributes(); } if (attributes == null) { throw new 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."); } } return attributes; }
解決:
//RequestInterceptor這個接口是fegin攔截器,在fegin發送請求之前都會走這個攔截器
@Component public class FeignConfig implements RequestInterceptor {
@Override public void apply(RequestTemplate requestTemplate) {
//你需要設置自己的請求時候的一些參數,下面是我項目中用到的, requestTemplate.header(SecurityConstants.FROM, SecurityConstants.FROM_IN); } }