Continue series about Java REST client, I’d like to share how to create a Java REST client using Resteasy Client API which is a Restful web service provided by JBoss.

Generally speaking, there are 2 approaches to create Java REST client with Resteasy

  • Leverage the JAX-RS 2.0 Client API implemented by JBoss
  • Use the Resteasy Proxy Framework

In this post, we focus on the first approach which leverages the JAX-RS 2.0 Client API.

1. Preparation

Let’s ssume 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 the given id or the book cannot be deleted.

1.5. Source code

The demo source code can be found on the Github.

We are going to use Resteasy Client API to communicate with the RESTful service above and its JSON message provider: resteasy-jackson2-provider to convert JSON responses to Java objects.

2. Create Java REST Client Using Resteasy Client API

Below are examples of creating Java REST client using Resteasy client library. We will try to 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)

We have created a ResteasyClient object, targeted it to a resource URI, build a request and execute the HTTP GET method against the resource.

After receiving the response, we call the readEntity method to map the response into an array of Books.

We create a simple main method to test the method.

The output of my console is:

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

As required from the above RESTful service, we create an instance of the ResteasyClient and post the book in  JSON format to the service.

We update the main method for testing.

The console output should be similar to below:

Note that the id of the created book is returned, and the status code is 201.The book is created successfully.

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

We have created a new instance of ResteasyClient class, targeted it to the URI of the book resource, build the request to execute the HTTP PUT. The book in JSON format and its id will be sent with the request.

We update the main method to test:

The output of my console:

Note that the returned book already has the name updated.

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

We have created a new client, targeted it with the URI of the book resource, appended the id of the book as the path of the request and invoked the delete method.

Here is our updated main method to test:

The output of my console is:

3. Conclusion

Above are some examples of creating Java REST client using Resteasy client API. There are two approaches to create Java REST client using the Resteasy client. The first approach, which we have just applied in this post, is pretty similar to Jersey client API. In next post, I will show how to create Java REST client with the Resteasy client, but with the 2nd approach: Resteasy Proxy Framework.

If you want to attempt to create Java REST client with other frameworks such as Spring RestTempate, Jersey Client API, Apache HttpComponent and even java.net.URL package, you can see my previous posts here:

Java REST Client Using Retrofit 2

Java REST Client Using Unirest Java API

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 Netflix Feign

 

0 0 vote
Article Rating