如何在微服务架构中使用伪装客户端上载多个Multipart文件

时间:2019-10-03 01:34:12

标签: java microservices multipart spring-cloud-feign

我正在尝试使用伪装客户端上载多个多部分文件,但无法这样做。

经过几次研究, File Upload Using Feign - multipart/form-data

File upload spring cloud feign client

Array Multipart[] file upload using Feign client

客户端:

@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 [dispatcherServlet]的Servlet.service()抛出异常[请求处理失败;嵌套异常为feign.codec.EncodeException:无法写入请求:找不到根本原因的请求类型[[Lorg.springframework.web.multipart.MultipartFile;]和内容类型[multipart / form-data]]的合适的HttpMessageConverter feign.codec.EncodeException:无法编写请求:找不到适合请求类型[[Lorg.springframework.web.multipart.MultipartFile;]和内容类型[multipart / form-data]

的HttpMessageConverter

1 个答案:

答案 0 :(得分:0)

您应该在伪装配置期间设置编码器:

public class FeignSimpleEncoderConfig {
    @Bean
    public Encoder encoder() {
        return new FormEncoder();
    }
}