Continue the series about Java REST client, I’d like to share how to create Java REST client using Resteasy Client Proxy Framework which is provided by JBoss. As mentioned in my previous post: Java REST Client Using Resteasy Client, Resteasy Client Proxy Framework is another approach to create Java REST client besides the traditional way, which is leveraged JAX-RS 2.0 Client API(implemented by JBoss).

1. Preparation

Assume 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 given id or cannot delete the book.

1.5 Source code

The demo source code can be found on the Github. We will define a POJO to map with the Book response as below:

We 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 Proxy Framework

Below are examples of creating Java REST client using Resteasy client proxy framework. We will try to query, create, update and delete resources from the above REST service.

2.1. Define a Proxy Interface at the client side.

Be different from other frameworks like Spring RestTemplate, Apache HttpComponent, Jersey, Resteasy client proxy framework allows us to use JAX-RS annotations to invoke on a remote HTTP resource. We just need to define a Java interface and use JAX-RS annotations on methods and the interface. The framework will do the rest, take care of communicating with the resource, map the response body with the Java objects.

Here is the interface we will define to consume the REST API above.

Let’s take a look at several methods:

Because the REST API requires the HTTP GET method, we simply define the annotation @GET for the proxy method. We also specify the MediaType: application/json of the response from the REST API. The client proxy internals will convert the HTTP response to the List<Book> object.

Another method is updateBook

In similar to above method, since the REST API requires the HTTP PUT method, we define the annotation @PUT. We also specify the MediaType: application/json for both request and response. The @Path, @PathParam are also needed to be defined as required by the REST API.

2.2. Invoke methods on the Proxy

In this section, we will invoke methods we defined on the above Java interface to communicate with the resource.

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

We just need to create a ResteasyClient, targeted it to a resource, set the proxy interface and invoke the method we need on it.

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

Creating a book by calling the POST to a resource is also simple as the above method.

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

Same as creating the book.

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

3. Conclusion

We have learned how to create Java REST client using Resteasy Client Proxy Framework. It seems that this approach is more simple than using the traditional one, JAX-RS 2.0 Client API. On near future, I’ll introduce how to create Java REST client with other frameworks like Apache CXF, Netflix/Feign. Recently, I have posted some articles on the Java REST client topic, you can reference from following links.

Java REST Client Example

Java REST Client Examples Using OkHttp

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

0 0 vote
Article Rating