使用feign进行远程调用出现的问题(文件服务参数接收为null)
场景:使用若依系统个人中心页面上传头像失败
问题:前端上传头像后,后端system服务可以接收到值,但是远程调用file服务时,file服务接收到的参数为null
第一次尝试解决:
听从ai的建议
加了feign-form依赖
<!-- Feign Multipart Support --><dependency><groupId>io.github.openfeign.form</groupId><artifactId>feign-form-spring</artifactId><version>3.8.0</version></dependency>
创建配置类
/*** Feign multipart form支持配置*/
@Configuration
public class FeignMultipartSupportConfig {@Beanpublic Encoder feignEncoder() {return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters()));}
}
问题依然存在
第二次尝试解决
检查参数是否一致
api定义方法:
/*** 文件服务* * @author ruoyi*/
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
public interface RemoteFileService
{/*** 上传文件** @param file 文件信息* @return 结果*/@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
}
file服务文件上传方法:
/*** 文件上传请求*/@PostMapping("/upload")public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file) {...}
注意:需要使用@RequestPart注解。
feign拦截器类FeignRequestInterceptor加入过滤
/*** feign 请求拦截器* * @author ruoyi*/
@Component
public class FeignRequestInterceptor implements RequestInterceptor
{@Overridepublic void apply(RequestTemplate requestTemplate){...//解决图片上传 防止请求头Content-Type的boundary被更改if ("content-type".equals(name)) {continue;}...}}
加入过滤后,问题解决。