This tutorial is going to illustrate how to create a Java REST client using Jersey client API which is a standard Java-based API for communication with RESTful Web services. For more detail information, please visit Jersey official website.

1. Preparation

In this tutorial, we’re going to use Jersey client API to interact with a RESTful web service. Therefore, let’s assume that we have a RESTful web service with several API as follows:

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. Jersey Library Dependencies

We use Jersey client library for communicating with the RESTful API and its message provider Jersey-media-json-jackson to convert JSON to Java objects.

1.6. Sample Source Code

The sample source code presented in this tutorial is available on Github.

2. Create Java REST Client Using Jersey Client Library

Let’s get through some examples of creating Java REST client using Jersey client library. We will query, create, update and delete resources from the above REST service.

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

Let’s get to an example where we make an HTTP GET request to retrieve resource information from the RESTful web service:

We have created a Jersey client, targeted it with a resource path, build a request and invoke the HTTP GET method for the current request synchronously.

From the above response, we call the method “readEntity” to map the entity stream to an array of books.

And next, let see an example of testing the above method:

And the output of the console:

2.2. Make a POST request to the RESTful web service (Create a book)

Let’s take a look at an example where we use Jersey client API to make a POST request to the RESTful web service:

Because the RESTful API requires the POST method, we must call the post method on our request object. As for the posted entity, we create a new entity from our book and specify application/json as the MediaType.

Here is our updated main method for testing.

The output of the console is:

The id of the created book is returned, and the status code is 201 which is returned when the book is created successfully.

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

Next, we’re going to make a PUT request to the RESTful web service using Jersey client API:

We have created a Restful web service client, targeted it a resource path, append path(id of the book) to the URI of the current target instance. Then, we build a request, create an entity from our book with the application/json as the MediaType and invoke HTTP PUT method.

The main method to test is updated:

Let’s run the method “main” and see the output printed out:

2.4. Make a DELETE request to the RESTful web service (Delete a book)

Now, let’s see an example which we make a DELETE request to the RESTful web service using Jersey client API:

We have created a new client, targeted it a resource path, append path(id of the book) to the URI of the current target instance and invoke the delete method.

Here is our updated main method:

And the output to the console after running the method:

3. Conclusion

We have seen some examples how to create Java REST client using Jersey client API. Like other frameworks, Jersey client API facilitates consuming of RESTful API. If you want to try other examples with other frameworks such as Spring RestTempate, Apache HttpComponent, and even java.net.URL package, you can see my previous posts here:

Java REST Client Example

Java REST Client Examples Using OkHttp

Java REST Client Using Netflix Feign

Java REST Client Example 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 Resteasy Client Proxy Framework

Java REST Client Using Resteasy Client

 

 

 

 

0 0 vote
Article Rating