REST Client Using Jersey

Creating REST Client

In the previous tutorial, we created a simple REST service that responds to a HTTP GET call. We saw it working by opening the page in a browser and seeing the correct results. In this tutorial, we will see how to create a REST client that consumes the simple service that we created.

Jersey comes with a package javax.ws.rs.client which helps you to write a REST client for your REST server. We are using Jersey 2.x in our tutorials so we should let you know that the implementation could be slightly different from what it could be if we use Jersey 1.x.

Create a new simple Java Project named JerseyHTTPClient. Add the jersey jars to the project via Project Properties -> Java Build Path. For the MyFirstService, we can create a simple client named MyFirstClient which is as follows:

Make sure, first to run the JerseyRestExample project created in the previous tutorial on the Apache Tomcat server before running the above code so that you don’t get HTTP 404 Not Found Error. Output for the above program is as follows:


Response code is : 200
Result is as follows:
MyFirstService says Hello.

Let us see what is happening in the code above.

First, we obtain a client instance from a static method of ClientBuilder. ClientBuilder is a JAX-RS API which allows you to create instances of javax.ws.rs.client.Client. ClientBuilder can be used for adding any SSL settings if a secure connection is supported. The ClientConfig used in the code can register multiple filters and then the client can be created out of it.

After successfully creating the client, we create a WebTarget that targets the destined URL. Once we get the target for base URL, subsequent REST APIs can be called using target.path(subpath) call.

From this web target, we get an invocation builder. Required HTTP requests like get, put, post, delete, accept, etc. can be invoked via the invocation builder. If the GET request that is shown in code executes properly, we will get a response code of 200 OK. So, our code checks for the HTTP status code and proceeds to print the response content if the HTTP request was processed successfully.

Supporting Multiple MediaType Formats For The Same REST Service

In the introductory tutorial, we saw that Content negotiation could be done by REST client if the REST service supports multiple formats. We will now see how this can be done.

We need to change the implementation of MyFirstService that we created earlier to support multiple MediaTypes. In the following example, we are going to support HTML, XML and JSON formats in addition to PLAIN-TEXT that we supported in the previous tutorial.

Content Negotiation Of REST Client With Server

Now we have modified our MyFirstService REST Service to give the output in multiple formats. The output will be given according to what is specified by Client in the Accept header of the HTTP request. The following modifications to our initial REST Client created at the start of the tutorial will help us achieve this.

Sample input output for the above client program is as follows:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.