- 非web调用feign,RequestContextHolder.getRequestAttributes()是null的,所以需要在空的情况赋值一次
@Component
public class FeignConfig implements RequestInterceptor {
private static final Logger log = LoggerFactory.getLogger(FeignInterceptor.class);
public FeignInterceptor() {
}
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (servletRequestAttributes == null) {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
}
}
}
2.如果配置在父项目,子项目有时候不会被拦截(未找到原因)
/**
*继承父类的配置即可被拦截
*/
@Component
public class FeignConfig extends FeignConfig {
public FeignConfig() {
super();
}
@Override
public void apply(RequestTemplate requestTemplate) {
super.apply(requestTemplate);
}
}