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
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 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.
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 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class BookRepositoryImplResteasy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public Book[] getAllBooks() throws Exception { ResteasyClient client = new ResteasyClientBuilder().build(); Response response = client.target(URI_BOOK).request().get(); int status = response.getStatus(); System.out.println("Status code: " + status); Book[] books = response.readEntity(Book[].class); return books; } } |
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.
1 2 3 4 5 |
public static void main(String[] args) throws Exception { BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy(); Book books[] = bookRepository.getAllBooks(); System.out.println(books[0]); } |
The output of my console is:
1 2 |
Status code: 200 Book [id=3, name=Effective Java, author=Joshua Bloch] |
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 15 |
public class BookRepositoryImplResteasy { private static final String URI_BOOK = "http://localhost:8080/v1/books"; public Book createBook(Book book) throws Exception { ResteasyClient client = new ResteasyClientBuilder().build(); Response response = client.target(URI_BOOK).request() .post(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE)); int status = response.getStatus(); System.out.println("Status code: " + status); Book createdBook = response.readEntity(Book.class); return createdBook; } } |
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.
1 2 3 4 5 6 |
public static void main(String[] args) throws Exception { BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy(); Book book = new Book(null, "Effective Java", "Joshua Bloch"); Book createdBook = bookRepository.createBook(book); System.out.println(createdBook); } |
The console output should be similar to below:
1 2 |
Status code: 201 Book [id=3, name=Effective Java, author=Joshua Bloch] |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class BookRepositoryImplResteasy { 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).path(String.valueOf(book.getId())); Response response = target.request(MediaType.APPLICATION_JSON_TYPE) .put(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE)); int status = response.getStatus(); System.out.println("Status code: " + status); Book createdBook = response.readEntity(Book.class); return createdBook; } } |
We update the main method to test:
1 2 3 4 5 6 7 8 9 10 11 |
public static void main(String[] args) throws Exception { BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy(); // Getting the first book from the RESTful service Book book = bookRepository.getAllBooks()[0]; System.out.println(book); // Change the name book.setName(book.getName() + " 3rd"); // Then update the book book = bookRepository.updateBook(book); System.out.println(book); } |
The output of my console:
1 2 3 |
Book [id=3, name=Effective Java, author=Joshua Bloch] Status code: 200 Book [id=3, name=Effective Java 3rd, author=Joshua Bloch] |
Note that the returned book already has the name updated.
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 BookRepositoryImplResteasy { 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).path(String.valueOf(id)); Response response = target.request().delete(); System.out.println("Status code:" + response.getStatus()); } } |
Here is our updated main method to test:
1 2 3 4 5 6 |
public static void main(String[] args) throws Exception { BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy(); // Getting the first book from the RESTful service Book book = bookRepository.getAllBooks()[0]; bookRepository.deleteBook(book.getId()); } |
The output of my console is:
1 |
Status code:204 |
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