0

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]

Smriti mool
  • 139
  • 4
  • 11

1 Answers1

0

you should set the encoder during feign configuration:

public class FeignSimpleEncoderConfig {
    @Bean
    public Encoder encoder() {
        return new FormEncoder();
    }
}
sovannarith cheav
  • 743
  • 1
  • 6
  • 19
  • Still the same issue : Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]", – Smriti mool Oct 03 '19 at 04:02
  • I thinks you miss some point! Let's verify as below : ```@Configuration public class FeignSimpleEncoderConfig { @Bean public Encoder encoder() { return new FormEncoder(); } }``` And then use it in feign interface : ```@FeignClient(name = "file-server", configuration =FeignSimpleEncoderConfig.class) @RequestMapping public interface FileUploadService { ... } ``` – sovannarith cheav Oct 03 '19 at 06:10
  • Got NullPointerException. Are you sure the above code works? Do we need any dependencies? – Smriti mool Oct 03 '19 at 10:57
  • I used only this : ` org.springframework.cloud spring-cloud-starter-openfeign ` – sovannarith cheav Oct 04 '19 at 01:05
  • This is [sample project](https://github.com/sovannarithcheav/spring-cloud-feign-example.git) – sovannarith cheav Oct 04 '19 at 01:12
  • Seems like there is already a project in your m2 of io.github.openfeign.form. Also, when I try to fetch multipart file in user-service, zero files are retrieved. – Smriti mool Oct 09 '19 at 06:50