国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用Spring Cloud Feign上傳文件的示例

使用Spring Cloud Feign上傳文件的示例

2021-03-06 13:24周立 Java教程

本篇文章主要介紹了使用Spring Cloud Feign上傳文件的示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近經常有人問Spring Cloud Feign如何上傳文件。有團隊的新成員,也有其他公司的兄弟。本文簡單做個總結——

早期的Spring Cloud中,Feign本身是沒有上傳文件的能力的(1年之前),要想實現這一點,需要自己去編寫Encoder 去實現上傳?,F在我們幸福了很多。因為Feign官方提供了子項目feign-form ,其中實現了上傳所需的 Encoder 。

注:筆者測試的版本是Edgware.RELEASE。Camden、Dalston同樣適應本文所述。

加依賴

?
1
2
3
4
5
6
7
8
9
10
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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就能夠上傳啦。

注意點

?
1
2
3
4
@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的坑

示例如下:

?
1
2
3
4
5
6
7
8
9
@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); // 內容
 
}

說明:

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

坑:

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

填坑:

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

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

方案一:

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

在Spring中進行如下配置即可

?
1
2
3
4
5
6
7
8
9
10
11
@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中配置如下:

?
1
2
3
4
@Bean
public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
 return new FeignSpringFormEncoder(messageConverters);
}

推薦使用方案一

方案二為參考https://github.com/pcan/feign-client-test而來,未測

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.itmuch.com/spring-cloud-sum/spring-cloud-feign-upload/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人在线观看免费爱爱 | 夜夜操导航 | 亚洲免费在线看 | 亚洲精品短视频 | 99re免费视频精品全部 | 国产福利在线 | 一级黄片毛片 | 狠狠干狠狠操 | 亚洲视频在线观看网址 | 亚洲精品视频免费 | 天天亚洲综合 | 黄色毛片在线看 | www.99re| 亚洲国产精品久久久久久久 | 天天澡天天狠天天天做 | 天堂v视频 | 一级毛片免费完整视频 | 龙珠z国语291集普通话 | 欧美精品乱码久久久久久按摩 | 在线观看一区二区三区四区 | 亚洲aaa在线观看 | 日韩一区免费在线观看 | 国产精品久久久久久亚洲调教 | 超碰8| www.xxx日韩 | 日韩欧美成人一区二区三区 | 黄色一级片黄色一级片 | 亚洲国产成人精品女人久久 | 成人久久久久久久 | 精品久久久久国产 | 国产二区视频 | 精品在线播放 | 一区二区三区在线不卡 | 日韩精品 | 欧美日韩精品免费 | 黄色大片网站 | 亚洲夜幕久久日韩精品一区 | 成人影院av | 日韩国产在线观看 | 中文字幕一区二区在线观看 | 国产精品毛片久久久久久久 |