Thursday, May 1, 2014

Create a RESTful Web Service Client using Jersey and Eclipse - Tutorial

This tutorial shows how to create a RESTful web service client in Java using Eclipse and Jersey.

You may access the web service thru the browser but in this tutorial we will access the  RESTful web service thru a client application.

This new client application will test the web service created in this RESTful web service tutorial.

Development Environment
1. Eclipse IDE for Java EE Developers 4.3.2  (Kepler)  download
2. Jersey 2.7  download
3. JDK 1.7  download


1. Create a Java Project in Eclipse

Jersey provides a REST client library which we will use in this client application.

Create a new Java project.

File > New > Java Project




For project name enter:  RESTfulWSClient

Click Finish.

Create the package com.ericsoriano.restclient in your project's src folder.

Right-click on src folder > New > Package




For name enter:  com.ericsoriano.restclient

Click Finish.


2. Download Jersey and add Jersey Library to the project

If you don't have Jersey yet, download Jersey from here.

Unzip the downloaded Jersey zip file to your local directory.

Add all the jar files from Jersey's  api, ext and lib folders to your project and project build bath.

Right-click on project name > Build Path > Configure Build Path

You will get the Java Build Path page below.




Click 'Add Library' > Select 'User Library' > Next 

You will get the User Library page below.




Click on 'User Libraries'.

You will get the User Libraries page below.




Click 'New' > enter 'Jersey Library' > OK

Select 'Jersey Library' > Click 'Add External Jars' > add jars from Jersey's api folder

Select 'Jersey Library' > Click 'Add External Jars' > add jars from Jersey's ext folder

Select 'Jersey Library' > Click 'Add External Jars' > add jars from Jersey's lib folder

You will now have all the Jersey jars under your project's new Jersey Library.




Click OK.

You will then be back at the User Library page below.




Make sure the checkbox for 'Jersey Library' is checked.

Click Finish.

You should now be back on the Java Build Path page.




Make sure your newly created 'Jersey Library' is shown on the list.

Click OK.


3. Create your Java class

Create a new class "RESTClientDemo" under the package com.ericsoriano.restclient.

Copy the code below to your "RESTClientDemo" class.

package com.ericsoriano.restclient;


import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


public class RESTClientDemo {
 
 public static void main(String[] args) { 
  // create a Client instance
  Client client = ClientBuilder.newClient();
  
  // point the root web target to the context root (base URI) of the RESTful web service - http://localhost:8080/RESTfulWSDemo/rest
  WebTarget rootWebTarget = client.target("http://localhost:8080/RESTfulWSDemo/rest");
  
  // target the resource exposed thru the URI - http://localhost:8080/RESTfulWSDemo/rest/demo
  WebTarget resourceWebTarget = rootWebTarget.path("demo");
  
  // prepare a HTTP request - representation media type requested is "text/plain"
  Invocation.Builder invocationBuilderText = resourceWebTarget.request(MediaType.TEXT_PLAIN_TYPE);
  //invocationBuilder.header("some-header", "true"); -- use this if you want to setup headers for the request
  
  // prepare a HTTP request - representation media type requested is "text/plain"
  Invocation.Builder invocationBuilderXML = resourceWebTarget.request(MediaType.TEXT_XML_TYPE);  
  
  // build the request and send to remote resource (RESTful web service)
  Response responseText = invocationBuilderText.get();
  Response responseXML = invocationBuilderXML.get();
  
  System.out.println("Response status = " + responseText.getStatus());
  System.out.println("Response as TEXT: " + responseText.readEntity(String.class));
  System.out.println("Response as XML: " + responseXML.readEntity(String.class));
  
  
  // test a PUT request --------------------------------------------------
  
  // target the resource annotated with @PUT exposed thru the URI - http://localhost:8080/RESTfulWSDemo/rest/demo/{employeeId}
  WebTarget employeeWebTarget = resourceWebTarget.path("10");  
  
  // prepare a HTTP request - representation media type requested is "text/plain"
  Invocation.Builder invocationBuilderEmployee = employeeWebTarget.request(MediaType.TEXT_PLAIN_TYPE);
  
  // build the request and send to remote resource (RESTful web service) - value of request parameter {employeeId} is sent thru the .path() method above 
  Response responseToPutRequest = invocationBuilderEmployee.put(Entity.entity("", MediaType.TEXT_PLAIN));
  System.out.println("");
  System.out.println("PUT request - Response as TEXT: " + responseToPutRequest.readEntity(String.class));
  
 }
}





4. Run the Java class

Create and run first the web service from this RESTful web service tutorial.

Run as  java application the RESTClientDemo class in eclipse.

You should get the following output:


Response status = 200
Response as TEXT: Jersey REST Web Service works!
Response as XML: <?xml version="1.0"?><message> Jersey REST Web Service works!</message>

PUT request - Response as TEXT: Employee 10 record is updated!