Spring Cloud Feign如何上传文件

去查看原文

一、Feign官方提供了feign 的子项目feign-from

1、加依赖

io.github.openfeign.form feign-form 3.0.3 io.github.openfeign.form feign-form-spring 3.0.3

java开发工具下载地址及安装教程大全,点这里

更多深度技术文章,在这里

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();

}

}

}

如代码所示,在这个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)

(1)、produeces 、consumes 不能少

(2)、接口定义中的注解@RequestPart(value = “file”) 不能写成@RequestParam(value = “file”

(3)、最好将Hystrix的超时时间设长一点,例如5秒,否则可能文件还没上传完,Hystrix就超时了,从而导致客户端侧的报错

原创文章,转载请注明出处。

java开发工具下载地址及安装教程大全,点这里

更多深度技术文章,在这里

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容