[英]How to upload multiple Multipart files using feign client in Microservice architecture
我正在尝试使用 feign 客户端上传多个多部分文件,但我无法这样做。
经过一番研究, 使用 Feign 上传文件 - multipart/form-data
使用 Feign 客户端上传数组 Multipart[] 文件
客户端:
@FeignClient(name = "file-server", configuration = {FileUploadService.MultipartSupportConfig.class})
@RequestMapping
public interface FileUploadService {
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
public @ResponseBody
List<FileUploadResponseDTO> handleFileUpload(@RequestPart(name = "file") MultipartFile[] file);
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@Primary
@Scope("prototype")
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
我正在尝试访问的模块:
@PostMapping(value = "/upload", consumes = MULTIPART_FORM_DATA_VALUE)
@ApiOperation(UPLOAD_FILE)
public List<FileUploadResponseDTO> uploadFiles(@RequestPart(name = "file") MultipartFile[] file){
System.out.println("****hello ****");
return fileUploadService.uploadFiles(file);
}
以上对单个 Multipart 文件有效,但对多个文件显示以下错误:
Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败; 嵌套异常是 feign.codec.EncodeException:无法写入请求:没有为请求类型 [[Lorg.springframework.web.multipart.MultipartFile;] 和内容类型 [multipart/form-data]] 找到合适的 HttpMessageConverter,根本原因是假装。 codec.EncodeException:无法写入请求:没有为请求类型 [[Lorg.springframework.web.multipart.MultipartFile;] 和内容类型 [multipart/form-data] 找到合适的 HttpMessageConverter
您应该在 feign 配置期间设置编码器:
public class FeignSimpleEncoderConfig {
@Bean
public Encoder encoder() {
return new FormEncoder();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.