欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringCloud中怎么使用Feign上傳文件

Spring Cloud中怎么使用 Feign上傳文件,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

專注于為中小企業(yè)提供成都網(wǎng)站建設、成都做網(wǎng)站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)惠民免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

加依賴

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


編寫Feign Client

@FeignClient(name = "ms-content-sample", configuration = UploadFeignClient.MultipartSupportConfig.class)
public interface UploadFeignClient {
 @RequestMapping(value = "/upload", method = RequestMethod.POST,
   produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
   consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 @ResponseBody
 String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
 class MultipartSupportConfig {
  @Bean
  public Encoder feignFormEncoder() {
   return new SpringFormEncoder();
  }
 }
}

如代碼所示,在這個Feign Client中,我們引用了配置類MultipartSupportConfig ,在MultipartSupportConfig 中,我們實例化了SpringFormEncoder 。這樣這個Feign Client就能夠上傳啦。

注意點

@RequestMapping(value = "/upload", method = RequestMethod.POST,
   produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
   consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
中的produeces 、consumes 不能少;

接口定義中的注解@RequestPart(value = "file") 不能寫成@RequestParam(value = "file" 。

最好將Hystrix的超時時間設長一點,例如5秒,否則可能文件還沒上傳完,Hystrix就超時了,從而導致客戶端側的報錯。

SpringCloud中使用Feign的坑

示例如下:

@FeignClient("service-resource")
//@RequestMapping("/api/test")
public interface TestResourceItg {

 @RequestMapping(value = "/api/test/raw", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
 public String raw1(@PathVariable("subject") String subject, // 標題
     @RequestParam("content") String content); // 內(nèi)容

}

說明:

*使用RequestMapping中的consumes指定生成的請求的Content-Type
*RequestParam指定的參數(shù)會拼接在URL之后,如: ?name=xxx&age=18
*PathVariable指定的參數(shù)會放到一個LinkedHashMap<String, ?>傳入到feign的Encoder中進行處理,而在Spring中實現(xiàn)了該接口的Encoder為SpringEncoder,而該實現(xiàn)又會使用Spring中的HttpMessageConverter進行請求體的寫入。

坑:

*不要在接口類名上使用RequestMapping,雖然可以使用,但同時SpringMVC會把該接口的實例當作Controller開放出去,這個可以在啟動的Mapping日志中查看到
*使用默認的SpringEncoder,在不指定consumes時,PathVariable中的參數(shù)會生成JSON字符串發(fā)送,且默認情況下不支持Form表單的生成方式,原因為:FormHttpMessageConverter只能處理MultiValueMap,而使用PathVariable參數(shù)被放在了HashMap中。默認更不支持文件上傳。其實已經(jīng)有支持處理各種情況的HttpMessageConverter存在。

填坑:

*支持Form表單提交:只需要編寫一個支持Map的FormHttpMessageConverter即可,內(nèi)部可調(diào)用FormHttpMessageConverter的方法簡化操作。
*支持文件上傳:只需要把要上傳的文件封裝成一個Resource(該Resource一定要實現(xiàn)filename接口,這個是把請求參數(shù)解析成文件的標識),使用默認的ResourceHttpMessageConverter處理即可。
*支持處理MultipartFile參數(shù):編寫一個支持MultipartFile的MultipartFileHttpMessageConverter即可,內(nèi)部可調(diào)用ResourceHttpMessageConverter實現(xiàn),同時注意需要將其添加至FormHttpMessageConverter的Parts中,并重寫FormHttpMessageConverter的getFilename方法支持從MultipartFile中獲取filename
*所有的HttpMessageConverter直接以@Bean的方式生成即可,spring會自動識別添加

完美支持表單和文件上傳:

方案一:

使用附件中的MapFormHttpMessageConverter.java和MultipartFileHttpMessageConverter.java

在Spring中進行如下配置即可

@Bean
public MapFormHttpMessageConverter mapFormHttpMessageConverter(MultipartFileHttpMessageConverter multipartFileHttpMessageConverter) {
 MapFormHttpMessageConverter mapFormHttpMessageConverter = new MapFormHttpMessageConverter();
 mapFormHttpMessageConverter.addPartConverter(multipartFileHttpMessageConverter);
 return mapFormHttpMessageConverter;
}

@Bean
public MultipartFileHttpMessageConverter multipartFileHttpMessageConverter() {
 return new MultipartFileHttpMessageConverter();
}

方案二:

使用FeignSpringFormEncoder.java

在Spring中配置如下:

@Bean
public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
 return new FeignSpringFormEncoder(messageConverters);
}

看完上述內(nèi)容,你們掌握Spring Cloud中怎么使用 Feign上傳文件的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

文章名稱:SpringCloud中怎么使用Feign上傳文件
文章鏈接:http://aaarwkj.com/article22/psdocc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站維護云服務器、網(wǎng)頁設計公司、網(wǎng)站設計公司、自適應網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
亚洲最大成人综合福利网| 国产免费一级av剧情| 亚洲综合色视频在线播放| 91免费观看视频高清| 亚洲一区二区三区有码| 粉嫩国产av一区二区三区| 大胆丰满邻居少妇在线观看| 欧美日本国产老熟女视频| 亚洲国产欧美日韩久久| 又黄又爽又刺激的性视频| 欧美二区三区精品在线| 亚洲欧洲久久激情久av| 一区二区三区毛片免费| 日韩精品视频在线不卡播放 | av真人青青小草一区二区欧美 | 亚洲欧美日韩综合一区| 在线日韩中文字幕二区| 一卡二卡精品在线免费| 国产精品一区二区熟女| 成人激情视频在线观看| 国产丝袜美腿一二三区| 成人精品欧美欧美一级乱黄| 91在线直播观看高清| 国产精品久久亚洲一区二区| 不卡的国产在线视频| 高清在线一区二区在线| 91九色在线免费观看| 精品国产av一区二区麻豆| 免费啪啪视频一区二区| 欧美亚洲国产精品久久久| 婷婷色综合一区二区三区| 97在线视频这里只有精品| 99久久免费看国产精品| 97资源视频在线播放| 亚洲欧美中文字幕乱码| 欧美日韩国产在线91| 成人黄色av在线看| 欧美日韩av在线一区二区| 在线观看免费完整观看一区二区| 亚洲精品在线一二三区| 日韩一日韩一区二区三电影在线观看 |