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

使用feign傳遞參數(shù)類型為MultipartFile的問(wèn)題

 更新時(shí)間:2022年03月16日 11:46:58   作者:qq_43603116  
這篇文章主要介紹了使用feign傳遞參數(shù)類型為MultipartFile的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

feign傳遞參數(shù)類型為MultipartFile

feign默認(rèn)是不支持多媒體文件類型的文件傳輸?shù)模强梢酝ㄟ^(guò)引入第三方j(luò)ar包解決這個(gè)問(wèn)題,步驟可以分為三步。

引入maven依賴

?? ??? ?<dependency>
? ? ? ? ? ? <groupId>io.github.openfeign.form</groupId>
? ? ? ? ? ? <artifactId>feign-form</artifactId>
? ? ? ? ? ? <version>3.3.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>io.github.openfeign.form</groupId>
? ? ? ? ? ? <artifactId>feign-form-spring</artifactId>
? ? ? ? ? ? <version>3.3.0</version>
? ? ? ? </dependency>

加入配置類

@Configuration
public class FeignMultipartSupportConfig {
?
? ? @Bean
? ? @Primary
? ? @Scope("prototype")
? ? public Encoder multipartFormEncoder() {
? ? ? ? return new SpringFormEncoder();
? ? }
?
? ? @Bean
? ? public feign.Logger.Level multipartLoggerLevel() {
? ? ? ? return feign.Logger.Level.FULL;
? ? }
}

在feign客戶端進(jìn)行配置

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import config.FeignMultipartSupportConfig;
import feign.Response;
@FeignClient(value = "", fallback = FileServiceFallback.class,configuration=FeignMultipartSupportConfig.class)
public interface IFileService {
?? ?//上傳文件
?? ?@RequestMapping(value = "/rmi/fileService/mediaImgUpload", ?produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
?? ?public String mediaImgUpload(@RequestPart MultipartFile file);
?? ?//下載文件
?? ?@RequestMapping(value = "/rmi/fileService/mediaDownload",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
?? ?public Response mediaDownload(@RequestParam(required = true) String mediaId);

feign傳參MultipartFile問(wèn)題解決

首先,feign服務(wù)之間的調(diào)用,傳參默認(rèn)的格式為:ContentType=application/x-www-form-urlencoded

以表單的形式傳參的,而文件流的傳參,需要form-data的ContentType,否則會(huì)報(bào)錯(cuò)的

首先引入依賴

 <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.8.0</version>
        </dependency>
 
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.8.0</version>
        </dependency>

注意spring boot版本是2.x以上的,上面兩個(gè)依賴的版本不低于3.5.0,否則還是無(wú)效的

新建feign的配置

package com.wm.blog_config.config; 
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
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 org.springframework.context.annotation.Configuration; 
 
/**
 * @author :半卷流年
 * @description : 解決feign傳遞流數(shù)據(jù)的異常
 * @createTime :2020/6/14
 */
@Configuration
public class FeignSupportConfig {
 
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
 
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    } 
}

在feign接口中配置

package com.wm.blog_admin.feign; 
import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory;
import com.wm.blog_common.constatnt.CommonConstant;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.entity.TFile;
import com.wm.blog_common.req.TFileQuery;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.config.CustomFeignConfig;
import com.wm.blog_config.config.FeignSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; 
import java.util.List;
 
/***
 * @ClassName: PictureFeignClient
 * @Description: picture feign調(diào)用  todo feign使用get有坑啊,是否考慮使用HttpClient替換feign的HttpURLConnection,采用apache的HttpClient
 * @Author: wm_yu
 * @Create_time: 16:39 2020-3-26
 */
@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class)
public interface PictureFeignClient {
 
    /**
     * id查詢圖片信息
     * @param id
     * @return
     */
    @GetMapping("/web/picture/{id}")
    Result<TFileDO> get(@PathVariable("id") Long id); 
 
    /**
     * id批量查詢圖片信息
     * @param idList
     * @return
     */
    @PostMapping("/web/picture/getByIdList")
    Result<List<TFile>> getByIdList(@RequestBody List<Long> idList); 
 
    /**
     * 文件上傳
     * @param file
     * @return
     */
    @PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     Result<String> uploadFile(@RequestPart("file") MultipartFile file); 
}

注意加上這個(gè),表示傳參格式:

就可以傳參了的

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

相關(guān)文章

  • spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例

    spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例

    這篇文章主要介紹了spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • SpringBoot獲取Request和Response方法代碼解析

    SpringBoot獲取Request和Response方法代碼解析

    這篇文章主要介紹了SpringBoot獲取Request和Response方法代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 一文帶你學(xué)習(xí)Java中的線程

    一文帶你學(xué)習(xí)Java中的線程

    線程是系統(tǒng)調(diào)度的最小單元,一個(gè)進(jìn)程可以包含多個(gè)線程,線程是負(fù)責(zé)執(zhí)行二進(jìn)制指令的。本文將詳細(xì)給大家介紹一下Java中的線程,,需要的朋友可以參考下
    2023-05-05
  • 介紹Jersey-Jersey入門基礎(chǔ)

    介紹Jersey-Jersey入門基礎(chǔ)

    REST不是一種新的技術(shù),而僅僅是一個(gè)理論,實(shí)踐這樣的理論可以讓我們的應(yīng)用更加先進(jìn)。
    2013-02-02
  • 基于java實(shí)現(xiàn)DFA算法代碼實(shí)例

    基于java實(shí)現(xiàn)DFA算法代碼實(shí)例

    這篇文章主要介紹了基于java實(shí)現(xiàn)DFA算法代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解

    springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解

    這篇文章主要介紹了springboot?ErrorPageFilter的實(shí)際應(yīng)用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • java策略枚舉:消除在項(xiàng)目里大批量使用if-else的優(yōu)雅姿勢(shì)

    java策略枚舉:消除在項(xiàng)目里大批量使用if-else的優(yōu)雅姿勢(shì)

    這篇文章主要給大家介紹了關(guān)于Java徹底消滅if-else的8種方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2021-06-06
  • Java排序算法總結(jié)之冒泡排序

    Java排序算法總結(jié)之冒泡排序

    這篇文章主要介紹了Java排序算法總結(jié)之冒泡排序,較為詳細(xì)的分析了冒泡排序的原理與java實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • Mybatis中特殊SQL的執(zhí)行的實(shí)現(xiàn)示例

    Mybatis中特殊SQL的執(zhí)行的實(shí)現(xiàn)示例

    本文主要介紹了Mybatis中特殊SQL的執(zhí)行的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java實(shí)現(xiàn)撲克牌牌面小程序

    java實(shí)現(xiàn)撲克牌牌面小程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)撲克牌牌面小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11

最新評(píng)論

江口县| 朝阳市| 虹口区| 南雄市| 吉隆县| 慈溪市| 泗洪县| 淳安县| 博野县| 乌兰县| 宁强县| 邳州市| 青海省| 景泰县| 宁南县| 甘德县| 西乡县| 鹿邑县| 伊金霍洛旗| 蕲春县| 长子县| 吴旗县| 阳江市| 洪泽县| 祥云县| 新田县| 容城县| 辉县市| 遵化市| 曲阜市| 五寨县| 望江县| 汝城县| 兰州市| 绍兴市| 合江县| 阿拉尔市| 东源县| 永平县| 大名县| 怀宁县|