Spring Cloud Feign如何上傳文件

去查看原文
1、Feign官方提供了feign 的子項目feign-fromjava

一、加依賴git

io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3
java開發工具下載地址及安裝教程大全,點這裏
更多深度技術文章,在這裏github

二、編寫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就可以上傳了
注意:spring

@RequestMapping(value = 「/upload」, method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
(1)、produeces 、consumes 不能少app

(2)、接口定義中的註解@RequestPart(value = 「file」) 不能寫成@RequestParam(value = 「file」工具

(3)、最好將Hystrix的超時時間設長一點,例如5秒,不然可能文件還沒上傳完,Hystrix就超時了,從而致使客戶端側的報錯開發工具

原創文章,轉載請註明出處。
java開發工具下載地址及安裝教程大全,點這裏
更多深度技術文章,在這裏code

相關文章
相關標籤/搜索