如何使用Feign客户端上传多部分文件?

时间:2018-04-09 15:50:56

标签: spring-boot microservices netflix-feign spring-cloud-feign feign

我必须打电话将文件上传到以下服务: -

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public Response uploadFile(
       @RequestParam(value="file", required=true) MultipartFile file, 
       @RequestParam(value="asOfDate" Date asOfDate,
       @RequestHeader(value = "metric") String metric,
       @RequestHeader(value = "user_id") String userId,
       @RequestHeader(value = "user_name") String userName,
       @RequestHeader(value = "user_company_id") String userCompanyId){
 }

截至目前,我正在使用RestTemplate来调用此服务,但现在我需要将RestTemplate调用转换为Feign Client。

我试图使用假装完成多部分文件上传,但我似乎无法在任何地方找到一个很好的例子。

1 个答案:

答案 0 :(得分:0)

下面的代码应该可以工作

@FeignClient(
        url = "HOST_URL",
        name = "FileUploadClient")
public interface FileUploadClient {

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public Response uploadFile(
       @PathVariable(value="file", required=true) MultipartFile file, 
       @RequestParam(value="asOfDate" Date asOfDate,
       @RequestHeader(value = "metric") String metric,
       @RequestHeader(value = "user_id") String userId,
       @RequestHeader(value = "user_name") String userName,
       @RequestHeader(value = "user_company_id") String userCompanyId){
 }
}