0
votes

I'm trying to upload multiple multipart file using feign client, but I am not being able to do so.

After few research, File Upload Using Feign - multipart/form-data

File upload spring cloud feign client

Array Multipart[] file upload using Feign client

Client side:

@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));
        }
    }

Module I'm trying to access:

@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);
}

The above works fine for a single Multipart file but it shows following error for multiple files:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]] with root cause feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]

1

1 Answers

0
votes

you should set the encoder during feign configuration:

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