spring cloud feign 上傳文件報not a type supported by this encoder解決方案

上傳文件調用外部服務報錯:?not a type supported by this encoder

查看SpringFormEncoder類的源碼:

 1 public class SpringFormEncoder extends FormEncoder
 2 {
 3  
 4     public SpringFormEncoder()
 5     {
 6         this(((Encoder) (new feign.codec.Encoder.Default())));
 7     }
 8  
 9     public SpringFormEncoder(Encoder delegate)
10     {
11         super(delegate);//調用父類的構造方法
12         MultipartFormContentProcessor processor = (MultipartFormContentProcessor)getContentProcessor(ContentType.MULTIPART);
13         processor.addWriter(new SpringSingleMultipartFileWriter());
14         processor.addWriter(new SpringManyMultipartFilesWriter());
15     }
16  
17     public void encode(Object object, Type bodyType, RequestTemplate template)
18         throws EncodeException
19     {
20         if(!bodyType.equals(org/springframework/web/multipart/MultipartFile))
21         {
22             super.encode(object, bodyType, template);//調用FormEncoder對應方法
23             return;
24         } else
25         {
26             MultipartFile file = (MultipartFile)object;
27             java.util.Map data = Collections.singletonMap(file.getName(), object);
28             super.encode(data, MAP_STRING_WILDCARD, template);
29             return;
30         }
31     }
32 }

?

可以發現SpringFormEncoder的encode方法當傳送的對象不是MultipartFile的時候,就會調用super.encode, 也就是FormEncoder的encode方法。

FormEncoder類的部分源碼:

 1 public FormEncoder()
 2     {
 3         this(((Encoder) (new feign.codec.Encoder.Default())));
 4     }
 5  
 6     public FormEncoder(Encoder delegate)
 7     {
 8         _flddelegate = delegate;
 9         List list = Arrays.asList(new ContentProcessor[] {
10             new MultipartFormContentProcessor(delegate), new UrlencodedFormContentProcessor()
11         });
12         processors = new HashMap(list.size(), 1.0F);
13         ContentProcessor processor;
14         for(Iterator iterator = list.iterator(); iterator.hasNext(); processors.put(processor.getSupportedContentType(), processor))
15             processor = (ContentProcessor)iterator.next();
16  
17     }
18  
19     public void encode(Object object, Type bodyType, RequestTemplate template)
20         throws EncodeException
21     {
22         String contentTypeValue = getContentTypeValue(template.headers());//這里會去到@PostMapping中consumes的值,所以參數需要傳對象時指定一下consumes
23         ContentType contentType = ContentType.of(contentTypeValue);//為啥指定consumes,是因為不指定就是application/x-www-form-urlencoded,而且processors中也包含,為啥包含見FormEncoder的構造函數
24         if(!MAP_STRING_WILDCARD.equals(bodyType) || !processors.containsKey(contentType))
25         {
26             _flddelegate.encode(object, bodyType, template);//_flddelegate是啥呢,是SpringFormEncoder傳遞過來,也就是new Encoder.Default()
27             return;
28         }
29         Charset charset = getCharset(contentTypeValue);
30         Map data = (Map)object;
31         try
32         {
33             ((ContentProcessor)processors.get(contentType)).process(template, charset, data);
34         }
35         catch(Exception ex)
36         {
37             throw new EncodeException(ex.getMessage());
38         }
39     }

FormEncoderr的encode方法當傳送的對象是json格式的字符串的時候,就會調用?_flddelegate.encode,即Encoder.Default的encode方法,而這個Encoder.Default的encode方法判斷傳送的類型不是String或者byte[],就會拋異常

 1 public interface Encoder
 2 {
 3     public static class Default
 4         implements Encoder
 5     {
 6  
 7         public void encode(Object object, Type bodyType, RequestTemplate template)
 8         {
 9             if(bodyType == java/lang/String)
10                 template.body(object.toString());
11             else
12             if(bodyType == [B)
13                 template.body((byte[])(byte[])object, null);
14             else
15             if(object != null)//當我們用對象傳遞參數的時候,會走這里
16                 throw new EncodeException(String.format("%s is not a type supported by this encoder.", new Object[] {
17                     object.getClass()
18                 }));
19         }
20  
21         public Default()
22         {
23         }
24     }
25  
26  
27     public abstract void encode(Object obj, Type type, RequestTemplate requesttemplate)
28         throws EncodeException;
29  
30     public static final Type MAP_STRING_WILDCARD = Util.MAP_STRING_WILDCARD;
31  
32 }

?

解決方案一:繼續使用前面提到的方案,如果引用該配置類的FeignClient中,沒有使用實體類作為參數的接口,則去掉配置類上的注解@Configuration就可以了,去掉注解@Configuration之后,該配置就只對通過configuration屬性引用該配置的FeignClient起作用(或者將該文件上傳接口單獨放到一個FeignClient中,去掉配置類上的注解@Configuration)。

方案一只支持文件上傳,如果引用該配置的FeignClient中有使用實體類作為參數接收的接口,則調用該接口時會拋異常。

解決方案二:繼續使用前面提到的方案,將配置文件修改為如下:

 1 @Configuration
 2 class MultipartSupportConfig {
 3     @Autowired
 4     private ObjectFactory<HttpMessageConverters> messageConverters;
 5         
 6     @Bean
 7     public Encoder feignFormEncoder() {
 8         return new SpringFormEncoder(new SpringEncoder(messageConverters));
 9     }
10  }

方案二既支持文件上傳也支持實體類作為參數接收。

?

轉載于:https://www.cnblogs.com/UniqueColor/p/9647776.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396849.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396849.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396849.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

counter 計數器

包含了兩個屬性和一個方法&#xff1a; 1. counter-reset2. counter-increment3. counter()/counters()counter-reset&#xff08;主要作用就是給計數器起個名字。如果可能&#xff0c;順便告訴下從哪個數字開始計數。默認是0&#xff09;&#xff1a;.xxx { counter-reset: sm…

linux中的變量文件路徑,Linux庫文件和Shell可執行程序命令文件搜索路徑變量的設置...

一、庫文件的搜索路徑&#xff1a;1、在配置文件/etc/ld.so.conf中指定動態庫搜索路徑(需要添加其它庫文件的路徑&#xff0c;在文件的最后添加具體的路徑即可 [ 如&#xff1a;/usr/local/lib ]&#xff0c;添加后保存退出&#xff0c;然后在命令行ldconfig2、通過環境變量LD_…

消息隊列NetMQ 原理分析2-IO線程和完成端口

目錄 前言介紹目的IO線程初始化IO線程Proactor啟動Procator線程輪詢處理socketIOObject總結前言 介紹 [NetMQ](https://github.com/zeromq/netmq.git)是ZeroMQ的C#移植版本,它是對標準socket接口的擴展。它提供了一種異步消息隊列,多消息模式,消息過濾&#xff08;訂閱&#xf…

django——url(路由)配置

URL是Web服務的入口&#xff0c;用戶通過瀏覽器發送過來的任何請求&#xff0c;都是發送到一個指定的URL地址&#xff0c;然后被響應。 在Django項目中編寫路由&#xff0c;就是向外暴露我們接收哪些URL的請求&#xff0c;除此之外的任何URL都不被處理&#xff0c;也沒有返回。…

VC連接mysql數據庫錯誤:libmysql.lib : fatal error LNK1113: invalid machine 解決方法

VC連接MySQL的配置過程在上一篇博文中&#xff0c;不過當你設置好&#xff0c;以為萬事大吉的時候&#xff0c;運行卻出現這個錯誤&#xff1a;libmysql.lib : fatal error LNK1113: invalid machine type。 無效的機器類型&#xff0c;真的是很讓人捉急。 發生這個錯誤的原因是…

linux 內存泄漏 定位,一種內存泄露檢查和定位的方法

一個系統后臺服務進程&#xff0c;可能包括多個線程&#xff0c;在生成環境下要求系統程序能夠穩定長時間穩定運行而不宕機。其中一個基本的前提就是需要保證系統程序不存在內存泄露。那么&#xff0c;該如何判讀系統程序是否存在內存泄露呢&#xff1f;如果存在&#xff0c;又…

python怎么發送郵件_在Python如何使用SMTP發送郵件

SMTP&#xff08;Simple Mail Transfer Protocol&#xff09;即簡單郵件傳輸協議,它是一組用于由源地址到目的地址傳送郵件的規則&#xff0c;由它來控制信件的中轉方式。 python的smtplib提供了一種很方便的途徑發送電子郵件。它對smtp協議進行了簡單的封裝。 Python創建 SMTP…

統計單詞個數(劃分型)

codevs 1040 統計單詞個數 2001年NOIP全國聯賽提高組 題目等級 : 黃金 Gold題目描述 Description給出一個長度不超過200的由小寫英文字母組成的字母串(約定;該字串以每行20個字母的方式輸入&#xff0c;且保證每行一定為20個)。要求將此字母串分成k份(1<k<40)&#xff0c…

基于ASP.NET的新聞管理系統(三)代碼展示

5.1.1欄目部分 增加欄目&#xff08;addLanMu.aspx&#xff09;&#xff1a; <html xmlns"http://www.w3.org/1999/xhtml"> <head runat"server"> <title></title> <link rel"stylesheet" type"text/css" …

洛谷-求同構數的個數-NOIP2013提高組復賽

題目描述 Description 所謂同構數是指這樣的數&#xff0c;即它出現在它的平方數的右端。例如&#xff0c;5的平方是25 &#xff08;即5525&#xff09;&#xff0c;5是25右端的數&#xff0c;那么5就是同構數。又如&#xff0c;25的平方是625&#xff08;即2525625&#xff09…

plex linux 數據目錄,shareplex日常維護文檔

2017/07/25##SharePlex日常維護源(SRC)&#xff1a;192.168.1.101 db01目標(TGT):192.168.1.102 db02SRC:su - oraclesp_ctrlshowqstatusshow capture detailshow read detailshow log reverseshow config --查看當前使用參數文件list config --羅列出所有的參數文件(使用和未使…

ifconfig命令找不到_02. Linux命令之查看網絡連接

1. 查看網絡連接數和端口使用 netstat 命令查看網絡連接情況netstat -anp參數&#xff1a;-a 顯示所有選項-t (tcp)僅顯示tcp相關選項-u (udp)僅顯示udp相關選項-n 拒絕顯示別名&#xff0c;能顯示數字的全部轉化成數字。-p 顯示建立相關鏈接的程序名關鍵列解釋:Proto 表示協議…

grep與egrep的區別

grep與egrep的區別&#xff1b; 在linux系統環境下&#xff0c;我們通常使用grep命令來過濾出需要的行而egrep確很少使用&#xff0c;他們的區別其實很簡單&#xff0c;grep默認不支持正則表達式&#xff0c;egrep默認支持正則表達式&#xff0c;egrep 等于 grep -E 命令。轉載…

python學習之模塊(pip),列表生成式,模塊操作mysql,excel

python基礎 生成式 列表生成式  格式 [表達式 for 表達式 in 迭代對象 (可加判斷)] 原&#xff1a; 1 res1 [] 2 for i in range(1,5): 3   res1.append(i) 4 print(res1) 改&#xff1a; 1 res2 [i for i in range(1,5)] 2 print(res2) 字典生成式  格式 {key:value f…

linux驅動read函數 copytouser,Linux驅動編程 step-by-step (五)主要的文件操作方法實現...

主要的文件操作方法實現文件操作函數有很多的操作接口&#xff0c;驅動編程需要實現這些接口&#xff0c;在用戶編程時候系統調用時候會調用到這些操作structfile_operations {...loff_t (*llseek) (structfile *, loff_t,int);ssize_t (*read) (structfile *,char__user *,siz…

web開發中的 emmet 效率提升工具

web開發中的 emmet 效率提升工具 可以用來快速生成html 代碼。 并且給各種IDE、編輯器提供了插件支持&#xff0c;sublime &#xff0c;webstorm等。 如在webstorm中安裝好emmet之后&#xff0c;輸入以下文本&#xff0c; #page>div.content[ng-model"user"]ul>…

python二維數組操作_Python二維數組應用與操作

課課家將會在這里為大家詳細的介紹一下Python二維數組的相關應用方法以及定義方式&#xff0c;相信朋友們可以從中學習到更多的知識。 Python數組的應用中在實際編程中是一個非常重要的應用技術&#xff0c;作為Python編程人員來說&#xff0c;必須要熟練的掌握這方面的所有應用…

基于光線追蹤的渲染中景深(Depth of field)效果的實現

圖形學離線渲染中常用的透視攝像機模型時根據小孔成像的原理建立的&#xff0c;其實現通常是從向成像平面上發射ray&#xff0c;并把trace這條ray的結果作為成像平面上對應交點的采樣結果。即&#xff1a; 圖片來自《Fundamentals of Computer Graphics》 現實中的鏡頭拍攝的圖…

ubuntu 安裝 pycharm

添加源&#xff1a;$ sudo add-apt-repository ppa:mystic-mirage/pycharm安裝收費的專業版&#xff1a;$ sudo apt update$ sudo apt install pycharm安裝免費的社區版&#xff1a;$ sudo apt update$ sudo apt install pycharm-community卸載&#xff1a;$ sudo apt remove p…

帶你制作百詞斬單詞表讀寫插件

上篇博文簡單的介紹了一下Chrome插件&#xff0c;今天就與大家分享一下我做的這款有實際意義的插件吧。 做這款插件主要是用百詞斬站點進行單詞學習時&#xff0c;遇到的一點點鬧心事兒。在單詞表中不能聽發音。也不能練習拼寫。所以才忍無可忍的做了這么一款插件。自我感覺還是…