This tutorial is going to cover how to create a Java REST client using Apache HttpClient.

1. Preparation

Let’s assume that we have a RESTful web service for managing a library 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 httpasyncclient for communicating with the RESTful API and use jackson-databind to convert Java objects to JSON and vice versa.

2. Create Java REST Client Using Apache HttpClient

Now, let’s get to some examples of creating Java REST client using Apache HttpClient. We’re going to query, create, update and delete a resource from the above RESTful web service.

2.1. Using Apache HttpClient to Make a GET request

Since the API supports the GET method, we create an HttpGet with the REST URI. If the StatusCode returned from the server is 200 (SC_OK), then we will use ObjectMapper of the Jackson library to convert the JSON response to an array of Book. For more detail about the Jackson and ObjectMapper, you can see this post: Convert Java Objects To JSON And Vice Versa

We update the main method to test the method as below:

The output in my console:

2.2. Using Apache HttpClient to make a POST request

In this example, we will use the 2nd API to create a book.

Because the REST API requires the POST method, we create an instance of the HttpPost class. And to convert the given book into JSON string, we leverage the ObjectMapper. If the StatusCode returned from the server is 201 (SC_CREATED), we convert back the response from JSON string to the book that has just been created. The book should have updated fields from the server.

We update the main method for testing.

The output of my console:

2.3. Using Apache HttpClient to make a PUT request

According to the API, to update a given Book, we have to issue an HTTP request with the HTTP PUT method to the RESTful API; therefore, we create an instance of HttpPut with the URI. Then we use the Jackson ObjectMapper to convert the object “book” into JSON format because that was required by the API.

And in similar to above example, if the StatusCode returned from the server is 20 (SC_OK), we convert back the response from JSON string to a Book object.

Let’s the main method after updating:

And output of the console after running it:

2.4. Using Apache HttpClient to make a DELETE request

To delete a book, we have to issue an HTTP request with DELETE method; so, we create a new instance of the HttpDelete class. We get back the response and check whether the StatusCode is 204 (SC_NO_CONTENT) or not.

We update the main method to test as below:

The output of my console is:

4. Conclusion

The tutorial has illustrated us how to implement a Java REST client using Apache HttpClient. Note that we’ve implemented those examples using httpasyncclient module while another module we can use is HttpClient. In near future, I will provide more examples of implementing RESTful client with other libraries, frameworks. Recently, I have had several posts on the same topic. Please refer following links:

Java REST Client Example

Simple Java REST Client Using java.net.URL package(using the built-in package java.net.URL of the JDK)

Java REST Client Using Spring RestTemplate

Java REST Client Using Resteasy Client

Java REST Client Using Jersey Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Netflix Feign

Java REST Client Examples Using OkHttp

0 0 vote
Article Rating