温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何解决feign微服务间的文件上传报错问题

发布时间:2021-06-30 15:23:18 来源:亿速云 阅读:134 作者:chen 栏目:开发技术

本篇内容介绍了“如何解决feign微服务间的文件上传报错问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

A微服务调用B服务的上传文件接口报错:

the request was rejected because no multipart boundary was found

spring cloud版本 H

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Hoxton.SR8</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

如果你是使用feign相信你已经引入openfeign的依赖了;

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

服务提供者接口:

//服务提供者上传文件接口
@PostMapping(value = " /file/upload")
public ResultModel<FileInfo> fileUpload(@RequestParam("access_token") String accessToken, @RequestParam("file") MultipartFile file) {
    //返回保存文件信息的实体类
    FileInfo fileInfo = fileInfoService.uploadFile(file);
    return ResultModel.ok(fileInfo);
}

消费者接口:

@Component
@FeignClient(value = "file-server")
public interface FileInfoFeignService {
    @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultModel<FileInfo> fileUpload(@RequestParam("access_token") String accessToken, @RequestPart("file") MultipartFile file);
}

必须加

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

MultipartFile file 前面的注解必须是@RequestPart

消费者controller类:

//消费者上传文件接口
@PostMapping(value = "/fileUpload")
public ResultModel<FileInfo> fileUpload(@RequestParam("access_token") String accessToken, @RequestPart("file") MultipartFile file) {
    return fileInfoFeignService.fileUpload(accessToken, file);
}

这样就完成了!

自己调试了很多遍,感觉其他的博客写的乱七八糟的。

feign上传文件踩坑及解决

通过 feign 调用文件服务提供者接口时,需传输 文件file ,服务调用者有时会报错误:

feign.FeignException$BadRequest: status 400 reading

如何解决feign微服务间的文件上传报错问题

服务提供者会报 Required request part 'file' is not present 错误。

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

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

如何解决feign微服务间的文件上传报错问题

示例

服务调用者

@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));

“如何解决feign微服务间的文件上传报错问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI