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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<dependency>
<groupId>
com.netflix.feign
</groupId>
<artifactId>
feign-core
</artifactId>
<version>
8.18.0
</version>
</dependency>
<dependency>
<groupId>
io.github.openfeign.form
</groupId>
<artifactId>
feign-form
</artifactId>
<version>
2.0.5
</version>
</dependency>
<dependency>
<groupId>
com.netflix.feign
</groupId>
<artifactId>
feign-jackson
</artifactId>
<version>
8.18.0
</version>
</dependency>
|
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:
1
2
3
4
5
6
7
|
public
interface
FileUploadResource
{
@RequestLine
(
"POST /upload"
)
@Headers
(
"Content-Type: multipart/form-data"
)
Response
uploadFile
(
@Param
(
"name"
)
String
name
,
@Param
(
"file"
)
File
file
)
;
}
|
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
class
UploadService
{
private
static
final
String
HTTP_FILE_UPLOAD_URL
=
"http://localhost:8080/v1"
;
public
boolean
uploadFile
(
String
fileName
,
File
file
)
{
FileUploadResource
fileUploadResource
=
Feign
.
builder
(
)
.
encoder
(
new
FormEncoder
(
new
JacksonEncoder
(
)
)
)
.
target
(
FileUploadResource
.
class
,
HTTP_FILE_UPLOAD_URL
)
;
Response
response
=
fileUploadResource
.
uploadFile
(
"Example file"
,
file
)
;
return
response
.
status
(
)
==
200
;
}
}
|
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:
1
2
3
4
5
6
7
8
|
@Test
public
void
testUploadFile
(
)
{
UploadService
uploadService
=
new
UploadService
(
)
;
File
file
=
new
File
(
"notes.txt"
)
;
assertTrue
(
file
.
exists
(
)
)
;
assertTrue
(
uploadService
.
uploadFile
(
"The optional file name"
,
file
)
)
;
}
|
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