pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • mybatis

  • mybatis-plus

  • Spring

  • SpringBoot

  • SpringSecurity

  • SpringCloud

    • 文档

      • SpringCloud-广告系统实战(七)----广告检索系统(加载全量索引)
      • SpringCloud-广告系统实战(三)---通用模块的开发
      • SpringCloud-广告系统实战(二)---搭建脚手架
      • SpringCloud-广告系统实战(五)---广告检索系统(微服务调用)
      • SpringCloud-广告系统实战(六)----广告检索系统(广告数据索引的设计与实现)
      • SpringCloud-广告系统实战(四)---投放系统的开发
      • SpringCloud-广告系统设计与实现(一)---概述
      • SpringCloud微服务实战(一)-简介
      • SpringCloud微服务实战(七)-异步和消息
      • SpringCloud微服务实战(七)-消息服务在电商中的实践
      • SpringCloud微服务实战(三)-服务注册与发现
      • SpringCloud微服务实战(二)-微服务介绍
      • SpringCloud微服务实战(六)-统一配置中心
      • SpringCloud微服务实战(十)-Hystrix
      • SpringCloud微服务实战(四)-微服务中的服务拆分
    • Gateway

    • Spring Cloud Stream

    • Alibaba Sentinel 规则参数总结
    • Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】
    • Feign常见问题总结
    • SentinelResource注解 属性总结
    • Spring Cloud Alibaba Sentienl相关配置项
    • SpringCloudAlibaba
    • SpringCloud入门
    • 使用Spring Cloud Feign上传文件
      • 加依赖
      • 编写Feign Client
      • 注意点
    • 如何使用Feign构造多参数的请求
    • 实用技巧:Hystrix传播ThreadLocal对象(两种方案)
    • 扩展Ribbon支持Nacos权重的三种方式
    • 扩展Ribbon支持基于元数据的版本管理
    • 搭建生产可用的Nacos集群
  • 单元测试框架Mockito
  • 框架
  • SpringCloud
pursuewind
2020-11-23
目录

使用Spring Cloud Feign上传文件

# 使用Spring Cloud Feign上传文件

最近经常有人问Spring Cloud Feign如何上传文件。有团队的新成员,也有其他公司的兄弟。本文简单做个总结——

早期的Spring Cloud中,Feign本身是没有上传文件的能力的(1年之前),要想实现这一点,需要自己去编写Encoder 去实现上传。现在我们幸福了很多。因为Feign官方提供了子项目feign-form (opens new window) ,其中实现了上传所需的 Encoder 。

注:笔者测试的版本是Edgware.RELEASE。Camden、Dalston同样适应本文所述。

# 加依赖

<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>
1
2
3
4
5
6
7

# 编写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();
        }
    }
}
1
2
3
4
5
6
7
8

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

    中的produeces 、consumes 不能少;

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

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

Last Updated: 2023/01/30, 11:01:00
SpringCloud入门
如何使用Feign构造多参数的请求

← SpringCloud入门 如何使用Feign构造多参数的请求→

Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜
  • 网络DJ