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

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

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

Feign實(shí)現(xiàn)多文件上傳,Open Feign多文件上傳解決

廢話不多說(shuō),直接上代碼

  • 用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)了問(wèn)題

解決這個(gè)問(wèn)題可以重寫(xiě)SpringFormEncoder這個(gè)類

重寫(xiě)后的代碼如下:

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)多文件上傳,重寫(xiě)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)的問(wè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?SpringSecurity?JWT實(shí)現(xiàn)系統(tǒng)安全策略詳解

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

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

    新建一個(gè)springboot單體項(xiàng)目的教程

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

北宁市| 峡江县| 乌兰县| 弥勒县| 印江| 桐庐县| 磐安县| 兴山县| 浮梁县| 榆树市| 如东县| 三明市| 吉水县| 即墨市| 福安市| 治县。| 朝阳区| 平谷区| 察隅县| 乌拉特中旗| 峨眉山市| 鹿泉市| 安西县| 隆林| 乐陵市| 湘乡市| 通城县| 阳新县| 同仁县| 都江堰市| 饶河县| 大兴区| 河间市| 永吉县| 哈巴河县| 保德县| 阳西县| 大埔区| 临泉县| 丹凤县| 泗水县|