This tutorial is going to cover about how to develop a Java REST client example with Retrofit 2 which is a Type-safe HTTP client for Android and Java by Square.

1. Preparation

Let’s assume that we have a RESTful web service with several API as below. And we’re going to use Retrofit 2 API to create several basic Java REST client examples which will communicate with this RESTful web service.

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 example source code can be found on the Github.

We will use Retrofit 2 httpclient to communicate with the Rest API. Hence, we need to include Retrofit library in the build path. The Maven dependency for Retrofit is:

Note that we also included gson converter library into the build path so that we can specify the GsonConverterFactory for Retrofit to use later. And both versions of Retrofit and Converter-gons are 2.1.0.

We also define a POJO to map with the Book response as below:

2. Java REST Client Example With Retrofit 2

In this section, we will use Retrofit 2 API to create some Java REST client examples including create a resource, update a resource, query resource and delete a resource from REST API.

2.1. Define a service interface at the client side

This interface should contain all the methods we need to call/invoke from the REST API. As mentioned above, in this tutorial, we’re going to define 4 methods for the BookResourceService as below:

Let’s take a look at several methods:

This method is used to get all books available on the server. Because the REST API requires the HTTP GET method, we simply define the annotation @GET for the interface method. We can also define the headers for the request by using annotation @Headers

Call is An invocation of a Retrofit method that sends a request to a web server and returns a response. It can be executed synchronously with execute(), or asynchronously with enqueue(Callback<T> callback)

Another method that we will take a look is:

Because the REST API requires HTTP POST method for creating a resource, we annotate this method by the @POST annotation. We also put the @Body annotation to the Book object that we want Retrofit to serialize it and set the result directly as request body by using Gson converter that we defined above.

2.2. Invoke methods on the interface

In this section, we are going to invoke the methods we defined on the above Java interface to communicate with the REST API.

2.2.1. Make a GET request to the REST API (Get all books)

We have just created an instance of Retrofit by setting the REST API base URL and GsonConverterFactory, and then we create an instance of the BookResourceService that we have just defined above. Finally, we invoke the mode of the service and get back the body which is automatically converted into a list of Book.

2.2.2. Make a POST request to the REST API (create a book)

In similar to the GET method, we just need to call the updateBook of the service, then get back the body which was converted to a Book object.

2.2.3. Make a PUT request to the REST API (Update a book)

2.2.4. Make a DELETE request to the REST API (delete a book)

3. Conclusion

We have learned how to create Java REST client example with Retrofit 2, an HTTP client for Java and Android. In similar to other Rest client proxy frameworks such as Resteasy, Netflix Feign, etc. Retrofit 2 simplifies and abstracts the communication with REST API. Beside the Retrofit 2, there are a lot of approaches to creating Java REST client, you can refer to the following for more detail.

How to Post with Retrofit 2

How To Post JSON With Retrofit

Java REST Client Example

Java REST Client Examples Using OkHttp

Java REST Client Using Netflix Feign

Java REST Client Using Apache HttpClient

Java REST Client Using Spring RestTemplate

Simple Java REST Client

Java REST Client With 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