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
1 |
GET http://localhost:8080/v1/books |
1.2 Create a new book
1 |
POST http://localhost:8080/v1/books |
Example:
1 2 3 4 |
{ "name": "Java How To Program", "author": "Paul Deitel" } |
Responses: application/json
Example:
1 2 3 4 5 |
{ "id": 5 "name": "Java How To Program", "author": "Paul Deitel" } |
STATUS 201 if the book is created successfully.
1.3 Update a book
1 |
PUT http://localhost:8080/v1/books/{id} |
Example:
1 2 3 4 5 |
{ "id": 1, "name": "Java How To Program 2nd", "author": "Paul Deitel" } |
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
1 |
DELETE http://localhost:8080/v1/books/{id} |
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:
1 2 3 4 5 6 7 |
@JacksonXmlRootElement(localName = "book") public class Book { private Long id; private String name; private String author; // All getters and setters } |
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.
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.17.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson2-provider</artifactId> <version>3.0.17.Final</version> </dependency> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import com.howtoprogram.domain.Book; public interface SimpleResteasyProxyClient { @GET @Consumes("application/json") List<Book> getAllBooks(); @POST @Consumes("application/json") @Produces("application/json") Book createBook(Book book); @PUT @Path("/{id}") @Consumes("application/json") @Produces("application/json") Book updateBook(@PathParam("id") Long id, Book book); @DELETE @Path("/{id}") @Consumes void deleteBook(@PathParam("id") Long id); } |
Let’s take a look at several methods:
1 2 3 |
@GET @Consumes("application/json") List<Book> getAllBooks(); |
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
1 2 3 4 5 |
@PUT @Path("/{id}") @Consumes("application/json") @Produces("application/json") Book updateBook(@PathParam("id") Long id, Book book); |
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)
1 2 3 4 5 6 7 8 9 10 11 |
public class BookRepositoryImplResteasyProxy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public List<Book> getAllBooks() throws Exception { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(URI_BOOK); SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class); return simpleClient.getAllBooks(); } } |
2.2.2. Make a POST request to the RESTful web service (create a book)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class BookRepositoryImplResteasyProxy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public Book createBook(Book book) throws Exception { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(URI_BOOK); SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class); Book createdBook = simpleClient.createBook(book); return createdBook; } } |
2.2.3. Make a PUT request to the RESTful web service (Update a book)
1 2 3 4 5 6 7 8 9 10 11 12 |
public class BookRepositoryImplResteasyProxy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public Book updateBook(Book book) throws Exception { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(URI_BOOK); SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class); Book updatedBook = simpleClient.updateBook(book.getId(), book); return updatedBook; } } |
2.2.4. Make a DELETE request to the RESTful web service (delete a book)
1 2 3 4 5 6 7 8 9 10 11 |
public class BookRepositoryImplResteasyProxy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public void deleteBook(Long id) { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(URI_BOOK); SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class); simpleClient.deleteBook(id); } } |
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 Examples Using OkHttp
Java REST Client Using Apache HttpClient
Java REST Client Using Spring RestTemplate
Java REST Client With Jersey Client
Java REST Client Using Resteasy Client