feign客户端上传图片报错”current request is not a multipart request”解决方案
使用Feign客户端上传图片时,经常会遇到“current request is not a multipart request”错误。这是因为Feign在默认情况下不会将请求转换为多部分请求。(multipart request),服务器无法正确接收文件数据。
解决方案:需要调整Feign接口和配置。
1. 使用 @RequestPart 注解:
在Feign接口法中使用 @RequestPart 注解将 MultipartFile 参数标记是多部分请求的一部分。 这告诉Feign将参数作为多个表单数据的一部分发送。
2. 启用表单编码器:
在Feign配置类中添加属性 feign.form.encoder.enabled=true 启用表单编码器。 这使得Feign能够正确编码多部分表单数据。
以下是修改后的Feign接口示例:
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class) public interface UploadClient { @PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String fileUpload(@RequestPart("file") MultipartFile file); }
FeignSupportConfig.class 应该包含 feign.form.encoder.enabled=true 例如:
@Configuration public class FeignSupportConfig { @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(); } }
通过以上步骤,Feign客户端可以正确发送多个请求,从而解决“current request is not a multipart request“错误,成功上传图片。 请确保您的服务器端也正确配置为处理多个表单数据。
以上是Feign客户端上传的图片报错。Current request is not a multipart request“怎么办?详情请关注图灵教育的其他相关文章!
