Feign支持文件上传

1.2. Source code and dependencies

Beside feign-core, we will use feign-form which is module that adds support for encoding application/x-www-form-urlencoded and multipart/form-data forms for Feign requests. Here are the Maven dependencies that will be used for our example:

2. File uploading with Open Feign

2.1. Define a Proxy Interface at the client side

Firstly, we need to define a proxy interface which contains a methods targeted with the REST API for uploading. Let’s see how the interface is defined as below:

We annotate the method by the @RequestLine annotation which specifies the HTTP POST method and the resource path of the API. We also use the @Headers annotation to specify the Content-Type of the request is multipart/form-data.

For more detail about defining proxy interface, you can refer to my recent post about implementing Java REST client using Feign.

2.2. Invoke methods on the Proxy

Now we will define a class which will invoke the method defined proxy interface.

In the above method, we use Feign utility class to create an instance of the above proxy interface (FileUploadResource) with properly encoder (FormEncoder for multi-part file uploading) and the URL of the REST API.

2.3. Verify

Let’s see an example unit test code for the uploadFile method:

3. Conclusion

The tutorial has just illustrated about file uploading with Open Feign. The source code can be found on the Github project or you can download it by clicking on the link java-examples.zip . It is an Maven based project, so it should be easy to import into IDE such as Eclipse, Intellij

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值