In this article, we will create a REST client using Jersey framework for testing GET, POST, PUT and DELETE operations.
This client will be invoking a service that is deployed locally on Tomcat server.
We created the RESTful Service created using Jersey and Hibernate in this article :
RESTful service using Jersey and Hibernate
The urls for the GET, POST, DELETE operations are :
http://localhost:8080/JerseyHibernateApp/webapi/employees
http://localhost:8080/JerseyHibernateApp/webapi/employees/create
http://localhost:8080/JerseyHibernateApp/webapi/employees/delete/{id}
First of all, let’s create an empty maven project using following archetype :
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.topjavatutorial.app -DartifactId=JerseyRestClient -DinteractiveMode=false
Import this project in Eclipse. Refer following article if you need help importing the project :
Import Maven project in Eclipse
Now, lets add jersey-client dependency in pom.xml :
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19</version> </dependency>
Create a package com.topjavatutorial.client and create a class TestEmployeeClient in it. Add the following methods in it for testing the various operations provided.
Rest client for testing the POST operation
In this test, we will try to add a record into Employee table using the POST method provided by the REST service.
Here is the code for testing the POST operation :
private static void addEmployee(){ String input = "{\"age\":21,\"name\":\"John Doe\"}"; Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees/create"); ClientResponse response = resource .type("application/json").post(ClientResponse.class, input); // check response status code if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } System.out.println(response); }
Here is the output of this method:
POST http://localhost:8080/JerseyHibernateApp/webapi/employees/create returned a response status of 200 OK
Rest client for testing the GET operation
Here is the code for testing the GET operation :
private static void getEmployee(){ Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees"); String response = resource.get(String.class); System.out.println(response); }
Here is the result:
[{“age”:21,”id”:1,”name”:”John Doe”}]
We can also test the GET operation by directly submitting the url in the browser.
Rest client for testing the DELETE operation
In this test, we will try to delete a record from Employee table providing the corresponding “id”.
Here is the client code for testing the DELETE operation :
public static void deleteEmployee(){ Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees/delete/1"); ClientResponse response = resource .type("application/json").delete(ClientResponse.class); System.out.println(response); }
Here is the output of this method:
DELETE http://localhost:8080/JerseyHibernateApp/webapi/employees/delete/1 returned a response status of 200 OK
If you run this method for an invalid “id”, it will return an error.
For example, here is the response for running deleteEmployee() for id =2.
DELETE http://localhost:8080/JerseyHibernateApp/webapi/employees/delete/2 returned a response status of 400 Bad Request
Here is the complete rest client program for your reference :
package com.topjavatutorial.client; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class TestEmployeeClient { public static void main(String[] args) { //addEmployee(); //getEmployee(); deleteEmployee(); } private static void getEmployee(){ Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees"); String response = resource.get(String.class); System.out.println(response); } private static void addEmployee(){ String input = "{\"age\":21,\"name\":\"John Doe\"}"; Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees/create"); ClientResponse response = resource .type("application/json").post(ClientResponse.class, input); // check response status code if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } System.out.println(response); } public static void deleteEmployee(){ Client c = Client.create(); WebResource resource = c.resource("http://localhost:8080/JerseyHibernateApp/webapi/employees/delete/2"); ClientResponse response = resource .type("application/json").delete(ClientResponse.class); System.out.println(response); } }
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post
#