异步调用Feign接口空指针问题原因:
Feign接口的Request 默认是主线程和子线程不共享的,当异步调用Feign接口会因为获取不到ServletRequestAttributes报空指针。
解决方案:
我们先获取到当前请求,再分享给子线程。
首先获取attributes :
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();//防止fegin获取不到当前请求
在线程中设置attributes :
RequestContextHolder.setRequestAttributes(attributes);
实例代码:
ExecutorService executorService = Executors.newSingleThreadExecutor();
List<ExportFamilyMasterImplForm> finalList = list;
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();//防止fegin获取不到当前请求
executorService.execute(()->{
RequestContextHolder.setRequestAttributes(attributes);
List<ExportFamilyMasterForm> excelList = familyMemberService.dataTransformation(finalList);
});