Spring Cloud 升级 Spring Boot 从 1.5.x 版本 至 2.1.x 版本,服务消费方带参数文件上传报错排查处理!
程序员文章站
2022-03-10 15:35:07
...
有段时间没有更新博客的内容了!
今天刚刚好对Spring Cloud 升级 Spring Boot 从 1.5.x 版本 至 2.1.x 版本,服务消费方带参数文件上传接口报错,报错内容如下!
the request was rejected because no multipart boundary was found
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
此错误是通过 Feign 调用接口时报错!Feign 如下!
@RequestMapping(value = "/handler", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
@RequestParam("basePath") String basePath);
服务消费方如下
@RequestMapping(value = "/handler", method = RequestMethod.POST)
public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
@RequestParam("basePath") String basePath) throws IOException {
log.debug(file.getOriginalFilename());
log.debug("" + hostId);
log.debug("" + labelId);
log.debug(basePath);
HRepertory hRepertory = uploadServiceImpl.uploadAndInsertHRepertory(file, hostId, labelId, basePath);
if (hRepertory == null) {
return RestResult.noData();
}
logger.info(hRepertory.toString());
return RestResult.ok(hRepertory);
}
在之前的 1.5.x 版本中没有问题,升级版本后报错!百度搜索,有提到过 Content-Type 不要定义便可解决此问题!
在 Feign 中已经定义了 consumes = MediaType.MULTIPART_FORM_DATA_VALUE 这个部分!
不定义 consumes 属性会导致服务提供方不知道如何序列化参数!
经过反复测试实验,最终通过修改 consumes 属性值上增加一个 + ";" 来最终实现文件、参数均可上传!
修改后的 Feign 如下
@RequestMapping(value = "/handler", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE
+ ";")
public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
@RequestParam("basePath") String basePath);
最终查看服务消费方获取请求时的 Content-Type :
到这里应该知道问题的缘由了
在Feign的 MultipartFormContentProcessor 源码中,其中有一段就专门是用来添加boundary