Unirest is lightweight HTTP request client libraries available in multiple languages including Java, .NET, Ruby, Node, Objective-C, etc. Like other REST client libraries, Unirest aims to  simplify making HTTP REST requests. In this tutorial, I’d like to share how to implement Java REST client using Unirest Java API.

1. Preparation

Assume that we have a REST API with several APIs 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 can not delete the book.

1.5 Source code

The demo source code can be found on the Github.

We will use Unirest Java API to communicate with above REST API. The dependencies we need directly in the pom file is:

2. Create Java REST Client Using Unirest Java API.

2.1. Serialization

As required from the above REST API, the request and response should be in JSON. Therefore, we may want a way to serialize our Java object into JSON when we send request to the service and deserialize the response to the Java objects automatically. To do that in Unirest Java API, we just need to set an the ObjectMapper  it as below.

We have set the Jackson ObjectMapper  for the Unirest in the constructor of our class. We just need to do 1 time for all the method calls. After that, we are ready to invoke methods on the Unirest.

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

We simply invoke the get method on the Unirest class and class the asObject method to deserialize the response into an array of Books.

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

To create a book or  call a POST the method on the remote resource, we just need to call the post method of the Unirest, specify the Content-Type of the request and the Media Type we accept for the response. In our example, both of them are application/json.

We also need to call the asObject method of the Unirest to deserialize the response to a Java object.

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

We’ve implemented an updateBook method which execute the PUT on the remote resource. To do that, we just need to call the put method of the Unirest, specify the Content-Type of the request and the Media Type we accept for the response, both of them are application/json.

Note that we have just modified the URL of the API to include the book id as the resource path

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

3. Conclusion.

We have seen some example about implementing Java REST client using Unirest Java API. In similar to other libraries, Unirest is very lightweight and simple. If you want to learn more about implementing Java REST client using other libraries such as: Netflix Feign, Jersey, Spring RestTemplate, etc, you can refer to following links:

Simple Java REST Client Using java.net.URL package

Java REST Client Using Spring RestTemplate

Java REST Client Using Apache Httpcomponents

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

Java REST Client Using Netflix Feign

Set Timeouts with Unirest for Java

4 1 vote
Article Rating