File upload spring cloud feign client

感情迁移 提交于 2019-12-03 21:48:28

After few days searching a solution I found this. You should start to add feign form for spring dependency :

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.3.0</version>
</dependency

Then your feign client need this spring form encoder :

@FeignClient(name="attachment-service",  configuration = {AttachmentFeignClient.MultipartSupportConfig.class}
 fallback=AttachmentHystrixFallback.class)
public interface AttachmentFeignClient {

@RequestMapping(value= {"upload"}, consumes = {"multipart/form-data"})
void upload(@RequestPart(name="file") MultipartFile file, @RequestParam(name="attachableId") Long attachableId, 
        @RequestParam(name="className") String className, @RequestParam(name="appName") String appName);

 public class MultipartSupportConfig {
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder();
    }
  }
}

I hope it will help someone.

TL;DR
Transform your MultiPartFile to MultiValueMap. See example below


The answer mentioned by @martin-choraine is the proper and the best answer to have your FeignClient method signature, the same as the actual endpoint signature that you are trying to call. However, there is a way around that does not require you to define a FormEncoder or add any extra dependency because in some applications you are not allowed to do that (enterprise shit); all what you need is to transform your MultipartFile to a MultiValueMap and it will perfectly work as the standard encoder will be able to serialize it.


Actual EndPoint to be called

@PostMapping(path = "/add", consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
 public MyResponseObject add(@RequestParam(name = "username") String username,
                             @RequestPart(name = "filetoupload") MultipartFile file) {
              Do something
}

The POST method in your FeignClient should look like this

@PostMapping(path = "/myApi/add", consumes = MULTIPART_FORM_DATA_VALUE, 
              produces = APPLICATION_JSON_VALUE)
 public MyResponseObject addFile(@RequestParam(name = "username") String username,
                           @RequestPart(name = "filetoupload") MultiValueMap<String, Object> file);

Then when you call the addFile you should provide MultiValueMap like this

public MyResponseObject addFileInAnotherEndPoint(String username, MultipartFile file) throws IOException {

    MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
    ByteArrayResource contentsAsResource = new ByteArrayResource(file.getBytes()) {
        @Override
        public String getFilename() {
            return file.getOriginalFilename();
        }
    };
    multiValueMap.add("filetoupload", contentsAsResource);
    multiValueMap.add("fileType", file.getContentType());
    return this.myFeignClient.addFile(username, multiValueMap);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!