一、Feign官方提供了feign 的子項(xiàng)目feign-from
1、加依賴
io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3
java開發(fā)工具下載地址及安裝教程大全,點(diǎn)這里。
更多深度技術(shù)文章,在這里。
2、編寫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();
}
}
}
如代碼所示,在這個(gè)Feign Client中,我們引用了配置類MultipartSupportConfig ,在MultipartSupportConfig 中,我們實(shí)例化了
SpringFormEncoder 。這樣這個(gè)Feign Client就能夠上傳了
注意:
@RequestMapping(value = “/upload”, method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
(1)、produeces 、consumes 不能少
(2)、接口定義中的注解@RequestPart(value = “file”) 不能寫成@RequestParam(value = “file”
(3)、最好將Hystrix的超時(shí)時(shí)間設(shè)長一點(diǎn),例如5秒,否則可能文件還沒上傳完,Hystrix就超時(shí)了,從而導(dǎo)致客戶端側(cè)的報(bào)錯(cuò)
原創(chuàng)文章,轉(zhuǎn)載請注明出處。
java開發(fā)工具下載地址及安裝教程大全,點(diǎn)這里。
更多深度技術(shù)文章,在這里。