RESTful web service is very popular today. With supporting from a lot of frameworks such as: Spring Data Rest, JerseyJBoss Resteasy, developing a RESTful web service is pretty easy for Java developer. Consuming a RESTful service from client side technologies like Javascript, JQuery is pretty easy too; however, there are some cases which we have to  call a RESTful service from our Java source code. It’s simple; however, searching on the Internet gives us a lot of examples implemented by different frameworks, and they requires a lot of dependencies of course. In this post, I’d like to show several examples how to implement Java REST client using java.net.URL package. This package was consisted in the JDK, and we don’t need to include any other dependencies on the classpath.

1. Preparation

Assume that we have an RESTful web services with several API as below:

1.1. List all books

Responses: application/json

1.2. Create a new book

Request

Example:

Responses

STATUS 201 if the book is created successfully.

1.3. Source code

The sample source code presented in this tutorial is available on my Github project.

Regarding the dependencies, we will use two Jackson libraries:

The first one is used for JSON and the second one is used for XML.

2. Create Java REST client using  java.net.URL package

2.1. Make a GET request to the RESTful web service.

We will get the list of books from the first  API.

The output from console is an array of book in JSON format.

2.2. Make a POST request to the RESTful web service.

We will create a new book using the second API.

The console output is:

2.3. Make a POST request with XML Content-Type to the RESTful web service.

Here is an example about how to make a POST request with XML Content-Type to the RESTful web service.

We assume that the above RESTful API support the Content-Type XML.  The source code is almost the same with posting the JSON Content-Type. We just need to set the property Content-Type of the connection object to “application/xml”  and replace the JSON content by XML content.

The console output is:

3. Improvements

We have seen some examples about Java REST client using java.net.URL package. Those examples are very basic and suitable for applications that need to make several calls to the a RESTful web service for notification purpose or trigger a remote action. If we need more interactions between our application and other RESTful web services, these ways may be not productive and efficiency. One reason we can see on above examples is how to convert our objects to JSON and XML strings to post them to the RESTful services.

We can improve this by using some libraries which will support us in automating the conversion back and forward such as: Jackson, Google Json, Json Lib. You can reference my previous post for more detail: Convert Java Objects To JSON And Vice Versa

So, we will try to improve our above examples by using Jackson library. We first need an Book class which its instances will be converted to JSON/XML and vice versa.

3.1. Make a GET request to the RESTful web service.

The source code for getting the list of books from the RESTful API is the same with the first example. After getting the JSON response from the RESTful API, we create a new ObjectMapper and convert the response to an array of book.

The output in the console is as below. Note that we already generated the toString() method of the Book class.

3.2. Make a POST request to the RESTful web service.

The source code is almost the same with the 2nd example above. In this example, instead  of creating a JSON string, we create an Book object. Converting the object to byte array is responsibility of the ObjectMapper. It will be more efficiency.

3.3. Make a POST request with XML Content-Type to the RESTful web service.

In this example, we also use the jackson-dataformat-xml package to convert the Java object to XML. In stead of creating an XML string, we create an instance of Book and use the XMLMapper object to convert the book to XML.

The output at client side is:

4. Conclusion

We have seen some examples about  implementing Java REST client using java.net.URL package of the JDK. This method is very simple, suitable for several calls to RESTful web services and requires no other dependencies in our classpath. There are some better libraries such as: SpringTemplate, Jersey client,etc, and I’d like to mention in the following posts:

Java REST Client Using Netflix Feign

Java REST Client Using Apache CXF Proxy based API

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Resteasy Client

Java REST Client Using Spring RestTemplate

Java REST Client Using Apache HttpClient

Java REST Client Using Jersey Client

 

0 0 vote
Article Rating