Feign实现多文件上传,Open Feign多文件上传解决方案
如何使用Feign实现多文件上传,Open Feign多文件上传解决方案
废话不多说,直接上代码
用feign多文件上传的Controller代码如下
@Slf4j
@RestController
@RequestMapping("/store")
@Api(description = "店铺管理接口", tags = "店铺管理接口")
public class StoreController {
@Autowired
private StoreService storeService;
@ApiOperation(value = "新增店铺信息")
@PostMapping(value = "/addStoreInfo")
public Result<Store> addStoreInfo(@Valid @ApiParam(value = "添加店铺时的店铺") StoreDto storeDto, MultipartFile[] multipartFiles) {
return storeService.addStoreInfo(storeDto,multipartFiles);
}
}
FeignClient代码如下
/**
* FileName: FileService
* Author: SixJR.
* Date: 2022/3/2 18:38:56
* Description: 文件RPC服务接口
* History:
* <author> <time> <version> <desc>
*/
@FeignClient(name = FeignServiceNameConstants.FILE_SERVICE, fallbackFactory = FileServiceFallbackFactory.class, decode404 = true)
public interface FileService {
@PostMapping(value = "/enclosure/upload/{objectName}/{objectId}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result upload(@PathVariable("objectName")String objectName, @PathVariable("objectId")String objectId,@RequestPart("multipartFiles") MultipartFile[] multipartFiles);
}
Feign调用服务,传送类似MultipartFile[] multipartFiles多文件的时候,会出现如下错误
Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]"
错误是因为Feign在组装MultipartFile[] multipartFiles多文件的时候出现了问题,解决这个问题可以重写SpringFormEncoder这个类,重写后的代码如下:
import feign.form.ContentType;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringFormEncoder;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;
/**
* FileName: SpringMultipartEncoder
* Author: SixJR.
* Date: 2022/3/2 19:54:12
* Description: Feign实现多文件上传,重写SpringFormEncoder
* History:
* <author> <time> <version> <desc>
*/
public class SpringMultipartEncoder extends SpringFormEncoder {
public SpringMultipartEncoder(Encoder delegate) {
super(delegate);
MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
processor.addWriter(new SpringSingleMultipartFileWriter());
processor.addWriter(new SpringManyMultipartFilesWriter());
}
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (bodyType != null && bodyType.equals(MultipartFile[].class)) {
MultipartFile[] file = (MultipartFile[]) object;
if(file != null) {
Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
}
}
super.encode(object, bodyType, template);
}
}
配置类如下
package com.chinared.common.config;
import com.chinared.common.utils.SpringMultipartEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import feign.codec.Encoder;
import org.springframework.context.annotation.Configuration;
/**
* FileName: MultipartSupportConfig
* Author: SixJR.
* Date: 2022/3/2 19:56:43
* Description: 解决Feign在组装MultipartFile[]的时候出现的问题
* History:
* <author> <time> <version> <desc>
*/
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
}
}
最后在FeignClient指定一下调用类就好啦~
package com.chinared.common.feign;
import com.chinared.common.config.MultipartSupportConfig;
import com.chinared.common.constant.FeignServiceNameConstants;
import com.chinared.common.feign.fallback.FileServiceFallbackFactory;
import com.chinared.common.model.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* FileName: FileService
* Author: SixJR.
* Date: 2022/3/2 18:38:56
* Description: 文件RPC服务接口
* History:
* <author> <time> <version> <desc>
*/
@FeignClient(name = FeignServiceNameConstants.FILE_SERVICE, fallbackFactory = FileServiceFallbackFactory.class, decode404 = true,configuration = MultipartSupportConfig.class)
public interface FileService {
@PostMapping(value = "/enclosure/upload/{objectName}/{objectId}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result upload(@PathVariable("objectName")String objectName, @PathVariable("objectId")String objectId,@RequestPart("multipartFiles") MultipartFile[] multipartFiles);
}
好啦,本篇关于OpenFeign支持多文件上传的解决方案就到这啦~
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgeiege
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01