Feign實(shí)現(xiàn)多文件上傳,Open?Feign多文件上傳問題及解決
Feign實(shí)現(xiàn)多文件上傳,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 = "添加店鋪時(shí)的店鋪") StoreDto storeDto, MultipartFile[] multipartFiles) { return storeService.addStoreInfo(storeDto,multipartFiles); } }
- FeignClient代碼如下
/** * FileName: FileService * Author: SixJR. * Date: 2022/3/2 18:38:56 * Description: 文件RPC服務(wù)接口 * 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調(diào)用服務(wù),傳送類似MultipartFile[] multipartFiles多文件的時(shí)候
會(huì)出現(xiàn)如下錯(cuò)誤
Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]"
錯(cuò)誤是因?yàn)镕eign在組裝MultipartFile[] multipartFiles多文件的時(shí)候出現(xiàn)了問題
解決這個(gè)問題可以重寫SpringFormEncoder這個(gè)類
重寫后的代碼如下:
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實(shí)現(xiàn)多文件上傳,重寫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[]的時(shí)候出現(xiàn)的問題 * 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指定一下調(diào)用類就好啦~
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服務(wù)接口 * 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); }
好啦,本篇關(guān)于OpenFeign支持多文件上傳的解決方案就到這啦~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot發(fā)送request請求的方式小結(jié)
在Java中,發(fā)送HTTP請求是常見需求,hutool工具包和RestTemplate類是實(shí)現(xiàn)此功能的兩種主流方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09解決出現(xiàn) java.lang.ExceptionInInitializerError錯(cuò)誤問題
這篇文章主要介紹了解決出現(xiàn) java.lang.ExceptionInInitializerError錯(cuò)誤問題的相關(guān)資料,需要的朋友可以參考下2017-01-01Spring Data MongoDB 數(shù)據(jù)庫批量操作的方法
在項(xiàng)目開發(fā)中經(jīng)常會(huì)批量插入數(shù)據(jù)和更新數(shù)據(jù)的操作,這篇文章主要介紹了Spring Data MongoDB 數(shù)據(jù)庫批量操作的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11maven配置多個(gè)鏡像的實(shí)現(xiàn)方法
這篇文章主要介紹了maven配置多個(gè)鏡像的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java的分支結(jié)構(gòu)與循環(huán)你知道多少
這篇文章主要為大家詳細(xì)介紹了Java的分支結(jié)構(gòu)與循環(huán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02java利用DFA算法實(shí)現(xiàn)敏感詞過濾功能
在最近的開發(fā)中遇到了敏感詞過濾,便去網(wǎng)上查閱了很多敏感詞過濾的資料,在這里也和大家分享一下自己的理解。下面這篇文章主要給大家介紹了關(guān)于java利用DFA算法實(shí)現(xiàn)敏感詞過濾功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06