There are many ways to create Java REST clients such as using the built-in java.net.URL package, leveraging client API of RESTful frameworks like Jersey, Resteasy, Spring Rest, etc. In this tutorial, we’d like to show how to create a Java REST client using Spring RestTemplate.

1. Preparation

Let’s assume that we have a RESTful web service with several APIs as follows:

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 source code presented in this tutorial is available on the Github.

About the library, we’re going to use some libraries:

1.6. POJO

Let’s define a POJO class “Book” to map with the above JSON:

2. Create A Java REST Client Using Spring RestTemplate

Let’s get to some examples of creating Java REST client using Spring RestTemplate. We’re going query, create, update and delete resources from the above REST service.

2.1. Using Spring RestTemplate to make a GET request

Firstly, let’s see how we use the Spring RestTemplate to issue an HTTP request to the first API to get all books:

We just need to specify the URL of the REST service and the ResponseType which is Book[].class in the example, and the Spring RestTemplate will convert the response into an array of Books.

We can test with a simple main method as below:

 

2.2. Using Spring RestTemplate to make a POST request

Secondly, we’re going to use the Spring RestTemplate to issue a request to the 2nd API to create a book:

We need to specify the URL of the RESTful service, the entity to be created and the ResponseType. Note that the API returns the STATUS 201 and the newly created book (includes the generated Id field) as JSON; however, we have used the postForObject method, we can not get the returned HTTP status. If we want to obtain the Http Status, we can use the postForEntity method as below:

If the RESTful API returns a URI for the newly created object, we can use another method: postForLocation of the Spring RestTemplate.

To test the method “createBook “, we can create a simple main method.

The output console is:

Note that the response includes the new field: id=4.

2.3. Using Spring RestTemplate to make a PUT request

And next, in the following example, we’re going to use the Spring RestTemplate to update a remote resource (Book) by making a PUT request to the RESTful API:

We have called the put method provided by the Spring RestTemplate class. And we can update the main method to test the updateBook method.

2.4. Using Spring RestTemplate to make a DELETE request

And finally, we’re going to use the Spring RestTemplate to delete or remove a remote resource by making a DELETE request:

We need to call the delete method of the Spring RestTemplate class, specify the URL and URL variables.

The main method will be updated to test as below:

3. Conclusion

The tutorial has illustrated us how to create a Java REST client using Spring RestTemplate. The tutorial just only covers some very basic methods provided by Spring RestTemplate. If you want to get to know more about other methods of the RestTemplate, you can refer to Spring RestTemplate site. In near future, I will provide more examples of implementing Java REST client with other libraries, frameworks.

Below are other related tutorials:

Simple Java REST Client Using java.net.URL package(using the built-in package java.net.URL of the JDK)

Java REST Client Using Resteasy Client

Java REST Client Using Jersey Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Netflix Feign

Java REST Client Using Apache HttpClient

0 0 vote
Article Rating