通过Feign上传文件

2022-07-30 10:19:25

通过feign 调用文件服务提供者接口时,需传输文件file ,服务调用者有时会报错误:feign.FeignException$BadRequest: status 400 reading
在这里插入图片描述
服务提供者会报Required request part 'file' is not present 错误。

这是因为服务调用者MultipartFile的value跟服务提供者@RequestPart中的value值不一样导致的。

在服务调用者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);
}

可以通过以下代码查看请求参数

Collection<Part> parts = request.getParts();
logger.info(JSONObject.toJSONString(parts, true));
  • 作者:如何下笔呢
  • 原文链接:https://blog.csdn.net/weixin_45768481/article/details/106267628
    更新时间:2022-07-30 10:19:25