使用feign傳遞參數(shù)類型為MultipartFile的問題
feign傳遞參數(shù)類型為MultipartFile
feign默認(rèn)是不支持多媒體文件類型的文件傳輸?shù)?,但是可以通過引入第三方j(luò)ar包解決這個(gè)問題,步驟可以分為三步。
引入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問題解決
首先,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)文章
java 利用HttpClient PostMethod提交json數(shù)據(jù)操作
這篇文章主要介紹了java 利用HttpClient PostMethod提交json數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-01-01Java?SM2加密相關(guān)實(shí)現(xiàn)與簡(jiǎn)單原理詳解
SM2算法可以用較少的計(jì)算能力提供比RSA算法更高的安全強(qiáng)度,而所需的密鑰長(zhǎng)度卻遠(yuǎn)比RSA算法低,這篇文章主要給大家介紹了關(guān)于Java?SM2加密相關(guān)實(shí)現(xiàn)與簡(jiǎn)單原理的相關(guān)資料,需要的朋友可以參考下2024-01-01Java?IO網(wǎng)絡(luò)模型實(shí)現(xiàn)解析
這篇文章主要為大家介紹了Java?IO網(wǎng)絡(luò)模型實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03詳解Java并發(fā)包中線程池ThreadPoolExecutor
ThreadPoolExecutor是Java語(yǔ)言對(duì)于線程池的實(shí)現(xiàn)。線程池技術(shù)使線程在使用完畢后不回收而是重復(fù)利用。如果線程能夠復(fù)用,那么我們就可以使用固定數(shù)量的線程來(lái)解決并發(fā)問題,這樣一來(lái)不僅節(jié)約了系統(tǒng)資源,而且也會(huì)減少線程上下文切換的開銷2021-06-06Java并發(fā)編程volatile關(guān)鍵字的作用
這篇文章主要介紹了Java并發(fā)編程volatile關(guān)鍵字的作用,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07java SelectableChannel的使實(shí)例用法講解
在本篇文章里小編給大家整理的是一篇關(guān)于java SelectableChannel的使實(shí)例用法講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03Java連接數(shù)據(jù)庫(kù)JDBC技術(shù)之prepareStatement的詳細(xì)介紹
這篇文章主要介紹了Java連接數(shù)據(jù)庫(kù)JDBC技術(shù)之prepareStatement的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07