Continue the series about Java REST client, I’d like to share how to create Java REST client using Apache CXF Proxy based API. In similar to the JBoss Resteasy Client Framework, there are several ways to implement REST client with Apache CFX client. The first one is to leverage the JAX-RS 2.0 Client API, the second is to use the Apache CFX Proxy-based API.

Java REST Client Using Apache CXF Proxy based API

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 use Apache CXF Client API to communicate with the RESTful service above, and we will use Jackson JSON providers to deal with JSON content.

2. Create Java REST Client Using Apache CXF Proxy based API

Below are examples of creating Java REST client using Apache CXF Proxy based API. 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.

In similar to the JBoss Resteasy Client Proxy Framework approach, we just need to define a Proxy Interface at client side using JAX-RS annotation to invoke on a remote HTTP resource. Apache CXF Proxy-based API will take care of communicating with the resource over HTTP protocol and try to map the response to Java objects.

Here is the Proxy Interface defined based on the specification of the above REST API.

The Book model is temporarily defined as below:

Let’s take a look at several methods of the BookResource

We just need to define the HTTP method required by the remote resource. This method will be intended to get all books from the resource; therefore, we use the HTTP GET method. We also want the response to be converted into a list of Books.

Another method is the updateBook

We defined this method based on the specification of the 3rd API which requires HTTP PUT method, the id of as the Path and the book as the request body.

2.2. Invoke methods on the Proxy

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

We use the JAXRSClientFactory utility class to create the BookResource proxy. Because the API produces the application/json response, we need to specify the JacksonJsonProvider for the proxy so that it can deal with that response. We also need to specify the MediaType: application/json as required by the REST API.

We create a simple main method to test as below:

The output of my console is:

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

It’s pretty similar to above method, we need to specify the JacksonJsonProvider, accept the application/json MediaType produced by the remote resource and especially the MediaType of the request.

We update the main method to test:

The output of my console:

The book that has just been created, has the new field: id = 4

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

We will update the main method to test.
Here is the output of my console:

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

We also update the main method to test:

3. Conclusion

We have learned how to create Java REST client using Apache CXF Proxy based API. Apache CXF Proxy-based API provides us an easy way to implement REST client. One special point we can see that we can re-use the server resource interface for the Proxy Interface at the client. Note that on above examples, we have used the JAXRSClientFactory utility class to create the proxy, and this proxy can be reused many times, we don’t need to create a proxy for each request. On next posts, I’d like to share more examples of how to implement Java REST client with other frameworks like Netflix Fiegn, Retrofit.

Recently, I have posted some articles on the Java REST client topic, if you’d like to try other frameworks, you can reference from following links.

Java REST Client 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 With Jersey Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Resteasy Client

Java REST Client Using Netflix Feign

 

0 0 vote
Article Rating