最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Feign實現(xiàn)多文件上傳,Open?Feign多文件上傳問題及解決

 更新時間:2022年11月23日 10:16:07   作者:By子諾  
這篇文章主要介紹了Feign實現(xiàn)多文件上傳,Open?Feign多文件上傳問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Feign實現(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 = "添加店鋪時的店鋪") 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多文件的時候

會出現(xiàn)如下錯誤

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多文件的時候出現(xiàn)了問題

解決這個問題可以重寫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實現(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[]的時候出現(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支持多文件上傳的解決方案就到這啦~

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot?SpringSecurity?JWT實現(xiàn)系統(tǒng)安全策略詳解

    SpringBoot?SpringSecurity?JWT實現(xiàn)系統(tǒng)安全策略詳解

    Spring?Security是Spring的一個核心項目,它是一個功能強(qiáng)大且高度可定制的認(rèn)證和訪問控制框架。它提供了認(rèn)證和授權(quán)功能以及抵御常見的攻擊,它已經(jīng)成為保護(hù)基于spring的應(yīng)用程序的事實標(biāo)準(zhǔn)
    2022-11-11
  • 新建一個springboot單體項目的教程

    新建一個springboot單體項目的教程

    這篇文章主要介紹了新建一個springboot單體項目的教程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Apache?Commons?Config管理配置文件核心功能使用

    Apache?Commons?Config管理配置文件核心功能使用

    這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java實現(xiàn)中英文詞典功能

    Java實現(xiàn)中英文詞典功能

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)中英文詞典功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java如何優(yōu)雅的實現(xiàn)微信登錄注冊

    Java如何優(yōu)雅的實現(xiàn)微信登錄注冊

    這篇文章主要給大家介紹了關(guān)于Java如何優(yōu)雅的實現(xiàn)微信登錄注冊的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • java 實現(xiàn)判斷回文數(shù)字的實例代碼

    java 實現(xiàn)判斷回文數(shù)字的實例代碼

    這篇文章主要介紹了java 實現(xiàn)判斷回文數(shù)字的實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot實現(xiàn)接口冪等性的4種方案

    SpringBoot實現(xiàn)接口冪等性的4種方案

    這篇文章主要介紹了SpringBoot實現(xiàn)接口冪等性的4種方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 淺談java中的一維數(shù)組、二維數(shù)組、三維數(shù)組、多維數(shù)組

    淺談java中的一維數(shù)組、二維數(shù)組、三維數(shù)組、多維數(shù)組

    下面小編就為大家?guī)硪黄獪\談java中的一維數(shù)組、二維數(shù)組、三維數(shù)組、多維數(shù)組。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • spring boot(三)之Spring Boot中Redis的使用

    spring boot(三)之Spring Boot中Redis的使用

    這篇文章主要介紹了spring boot(三)之Spring Boot中Redis的使用,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • Java行為型模式中命令模式分析

    Java行為型模式中命令模式分析

    在軟件設(shè)計中,我們經(jīng)常需要向某些對象發(fā)送請求,但是并不知道請求的接收者是誰,也不知道被請求的操作是哪個,我們只需在程序運(yùn)行時指定具體的請求接收者即可,此時可以使用命令模式來進(jìn)行設(shè)計
    2023-02-02

最新評論

巨野县| 蓬莱市| 潜江市| 喀喇沁旗| 津市市| 沅江市| 余干县| 尚义县| 石柱| 抚远县| 宣威市| 哈巴河县| 都兰县| 黔江区| 奉贤区| 浪卡子县| 兴仁县| 望城县| 穆棱市| 宜川县| 敦化市| 山西省| 兴和县| 云霄县| 刚察县| 北安市| 雷州市| 贵德县| 洛阳市| 襄城县| 平乡县| 海盐县| 马龙县| 淮安市| 富裕县| 松阳县| 大城县| 石棉县| 平阴县| 崇左市| 陵水|