通过Feign上传文件(踩坑)

通过 feign  调用文件服务提供者接口时,需传输 文件file ,服务调用者有时会报错误:feign.FeignException$BadRequest: status 400 reading  

通过Feign上传文件(踩坑)

 错误。

导致的。

在服务调用者MultipartFile的value要跟服务提供者的@RequestPart中的value值一样。不然它会抛出400异常!!!

示例

服务调用者

@PostMapping("/xxx/file")
public xx uploadOrderFilesToOSS(@ApiParam("附件") @RequestParam("file") MultipartFile[] file) {
   return xxxService.uploadOrderFilesToOSS(file);
}

Feign

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file);

服务提供者

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file) {
  return fileService.uploadFileToOSS(path, file);
}