This short tutorial is going to cover about file uploading with Open Feign, a java to http client binder.

1. Preparation

1.1. REST API for file uploading

Let’s assume that we have a RESTful web service which provides an API for file uploading as below:


Key Required Description
file required A binary file
name optional The name of the file

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 . It is an Maven based project, so it should be easy to import into IDE such as Eclipse, Intellij

4. References

Java REST Client Using Netflix Feign

Basic Authentication with Open Feign

Upload a File with OkHttp

Java REST Client Example

0 0 vote
Article Rating