Continue the series about Java REST client, I’d like to share how to implement Java REST client using Netflix Feign. As mentioned in the Feign site, Feign is a java to http client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. In similar to other REST client frameworks, libraries, Feign aims to simplify the interaction between client code and http api.

1. Preparation

Assume that we have a RESTful web service with several API as below:

1.1 Get all books

Responses: application/json

1.2 Create a new book

Request

Example:

Responses: application/json

Example:

STATUS 201 if the book is created successfully.

1.3 Update a book

Request

Example:

Responses: application/json

STATUS 200 if the book is updated successfully.

STATUS 400 if there is no book with given id

1.4 Delete a book

Responses: application/json

STATUS 204 if the book is deleted successfully.

STATUS 400 if there is no book with given id or cannot delete the book.

1.5 Source code

The demo source code can be found on the Github.

We use Netflix Feign to communicate with the REST service above, and we will use Jackson JSON providers to deal with JSON content.

2. Create REST Client Using Netflix Feign

2.1. Define a Proxy Interface at the client side.

We firstly base on the above RESTful API to create a Java interface, annotated with annotations provided by Feign. Feign will process those annotations to make requests and communicate with the remote http api.  Let’s see a BookResourceFeign class I have just defined as following:

We can see that the annotations are a little bit different with ones provided by JAX-RS, but it’s pretty easy to understand those. Because this client will accept response as JSON, we annotated the interface: @Headers(“Accept: application/json”). For the Post and Put resources, we specify the request body as JSON, so we annotated @Headers(“Content-Type: application/json”) on those methods. The values of the @RequestLine are kinds of text, very simple to understand.

As for the Book model, we have defined temporarily as follows:

2.2. Invoke methods on the Proxy

After having the proxy interface, we just need to invoke its methods to communicate with the RESTful API.

2.2.1. Make a GET request to the RESTful web service (Get all books)

We use Feign utility class to set Jackson Encoder, Decoder, target to the BookResourceFeign.class and resource URI to create a kind of proxy for that class.

We will create a main method to test as follows:

Here is the output of my console:

2.2.2. Make a POST request to the RESTful web service (create a book)

We simply invoked the createBook(book) method on the proxy to create new book by posting the JSON content to the REST API

We will update the main method to test:

The output of my console:

2.2.3. Make a PUT request to the RESTful web service (Update a book)

We update the main method to test:

Here is the output of my console:

Note that the book was updated with the new name: Java How To Program 3rd

2.2.4. Make a DELETE request to the RESTful web service (delete a book)

The main method is updated to test as below:

3. Conclusion

We have learned how to implement Java REST Client using Netflix Feign. Note that we have used Feign Jackson Json message converter to deal with JSON content from REST API. There are a lot of choices. We can use Gson, Sax, JAXB, JAX-RS if we prefer. In future, I will share how to implement Java REST client with another framework like Retrofit and summarize all the common frameworks, tools that can be used to implement Java REST client.

Below are other articles which related to the Java REST client for your references:

Using Netflix Feign with Spring Cloud

Basic Authentication with Open Feign

File Uploading with Open Feign

Java REST Client Using Retrofit 2

Simple Java REST Client Using java.net.URL package

Java REST Client Using Spring RestTemplate

Java REST Client Using Apache HttpClient

Java REST Client Using Jersey Client

Java REST Client Using Resteasy Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Apache CXF Proxy-based API

 

0 0 vote
Article Rating