欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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服務接口
 * 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)用服務,傳送類似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服務接口
 * 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支持多文件上傳的解決方案就到這啦~

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

相關文章

  • 淺析Java編程中類和對象的定義

    淺析Java編程中類和對象的定義

    下面小編就為大家?guī)硪黄獪\析Java編程中類和對象的定義。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-05-05
  • springboot發(fā)送request請求的方式小結(jié)

    springboot發(fā)送request請求的方式小結(jié)

    在Java中,發(fā)送HTTP請求是常見需求,hutool工具包和RestTemplate類是實現(xiàn)此功能的兩種主流方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • 解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題

    解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題

    這篇文章主要介紹了解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題的相關資料,需要的朋友可以參考下
    2017-01-01
  • 劍指Offer之Java算法習題精講排列與N叉樹

    劍指Offer之Java算法習題精講排列與N叉樹

    跟著思路走,之后從簡單題入手,反復去看,做過之后可能會忘記,之后再做一次,記不住就反復做,反復尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • Spring Data MongoDB 數(shù)據(jù)庫批量操作的方法

    Spring Data MongoDB 數(shù)據(jù)庫批量操作的方法

    在項目開發(fā)中經(jīng)常會批量插入數(shù)據(jù)和更新數(shù)據(jù)的操作,這篇文章主要介紹了Spring Data MongoDB 數(shù)據(jù)庫批量操作的方法,非常具有實用價值,需要的朋友可以參考下
    2018-11-11
  • Java中繼承、多態(tài)、重載和重寫介紹

    Java中繼承、多態(tài)、重載和重寫介紹

    這篇文章主要介紹了Java中繼承、多態(tài)、重載和重寫介紹,需要的朋友可以參考下
    2014-07-07
  • Java Springboot如何基于圖片生成下載鏈接

    Java Springboot如何基于圖片生成下載鏈接

    這篇文章主要介紹了Java Springboot如何基于圖片生成下載鏈接,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • maven配置多個鏡像的實現(xiàn)方法

    maven配置多個鏡像的實現(xiàn)方法

    這篇文章主要介紹了maven配置多個鏡像的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Java的分支結(jié)構(gòu)與循環(huán)你知道多少

    Java的分支結(jié)構(gòu)與循環(huán)你知道多少

    這篇文章主要為大家詳細介紹了Java的分支結(jié)構(gòu)與循環(huán),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • java利用DFA算法實現(xiàn)敏感詞過濾功能

    java利用DFA算法實現(xiàn)敏感詞過濾功能

    在最近的開發(fā)中遇到了敏感詞過濾,便去網(wǎng)上查閱了很多敏感詞過濾的資料,在這里也和大家分享一下自己的理解。下面這篇文章主要給大家介紹了關于java利用DFA算法實現(xiàn)敏感詞過濾功能的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06

最新評論