Learn in Java


Restful client using Jersey

In our Restful webservice using Jersey tutorial, we have created a restful webservice that contains two methods. Now, we will try to create a client and call the ping() method using Pure JAX-RS API without using Jersey and also using Jersey API.

Our test case file, HelloAngryWorldTest.java has 2 methods one using JAX-RS API and other using jersey :

1. HelloAngryWorldTest.java :

package com.learninjava;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

// Classes used by Pure JAX-RS API client
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;

// Classes used by Jersey API Client
//import com.sun.jersey.api.client.Client;
//import com.sun.jersey.api.client.ClientResponse;
//import com.sun.jersey.api.client.WebResource;
//import com.sun.jersey.api.client.config.ClientConfig;
//import com.sun.jersey.api.client.config.DefaultClientConfig;

import org.junit.BeforeClass;
import org.junit.Test;

 * @author learninjava.com
 * @see www.learninjava.com
public class HelloAngryWorldTest {
    private static String endpointUrl;

    public static void beforeClass() {
        endpointUrl = System.getProperty("service.url");

     * This method uses the Pure JAX-RS API to create a client.
     * Comment the following to run this client :
     * 1. testPingUsingJersey() in this file
     * 2. Jersey API imports in this file
     * 3. Jersey client dependencies in pom.xml
    public void testPingUsingPureJaxRS() throws Exception {
      Client client = ClientBuilder.newClient();
      WebTarget target = client.target(endpointUrl + "/helloAngryWorld/echo/Tweet");
      Invocation.Builder builder = target.request("text/plain"); 
      Response response = builder.get();
      assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
      String value = builder.get( String.class);
      System.out.println("Response from web service is : " + value);
      assertEquals("Tweet", value);

     * This method uses the Jersey API to create a client.
     * Comment the following to run this client :
     * 1. testPingUsingPureJaxRS() in this file
     * 2. Pure JAX-RS imports in this file
     * 3. Pure JAX-RS client dependencies in pom.xml
/*    @Test
    public void testPingUsingJersey() throws Exception {
      ClientConfig config = new DefaultClientConfig();
      Client client = Client.create(config);
      WebResource webresource = client.resource(endpointUrl + "/helloAngryWorld/echo/Tweet");    	
      ClientResponse response = webresource.accept("text/plain").get(ClientResponse.class);

      assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
      String value = webresource.get(String.class);
      System.out.println("Response from web service is : " + value);
      assertEquals("Tweet", value);

There are some inter dependencies between the jar files being used for Pure JAX-RS client API and Jersey Client API. Hence, let us try to run each client separately. That is, to run a method, we need to comment out the other and viceversa.

testPingUsingPureJaxRS() method uses pure JAX-RS classes for creating the response. Using pure JAX-RS API, the response creation will be as follows :

Client -> WebTarget -> Invocation.Builder -> Response

testPingUsingJersey() uses Jersey API, the response creation is as below :

Client -> WebResource -> ClientResponse

Testing the webservice:

There are number of ways you can test the Restful webservice. Before running any of the below, make sure the tomcat server is started and the webservice is running using,

mvn clean package -DskipTests=true tomcat7:run or
mvn clean package -DskipTests=true jetty:run

1. Using maven command :

i. Specify the service.url at command line :

mvn -Dservice.url=http://localhost:<port-number>/jaxrs-service test

This command uses a command line argument to specify the service.url. The service.url is used in the test case.

ii. Specify the service.url in pom.xml


Add the above configuration and use the below command to test the webservice,

mvn test

iii. Hard-code the service.url in HelloWorldTest.java and use the below command to run the tests.

mvn test

 T E S T S
Running com.learninjava.HelloAngryWorldTest
Response from web service is : Tweet
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 43.997 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Now try to run the testPingUsingJersey() method by commenting the JAX-RS related configuration and enabling the Jersey API configuration. You should see the same output as above. The idea is that you can use either way to create a Jersey Client.


2. Using Poster plugin :

Poster is a plugin for firefox browser. This is a very useful and handy tool to test your restful webservices quickly.

i. To test the ping method of HelloAngryWorld, enter the following in Poster UI :

URL          :   http://localhost:<port-number>/jaxrs-service/helloAngryWorld/echo/tweet
Content Type :   application/text

and click on GET button as ping is a GET request type. The response is displayed in the Response window.

ii. To test the modifyJson method of HelloAngryWorld, enter the following in Poster UI :

URL                         :  http://localhost:<port-number>/jaxrs-service/helloAngryWorld/jsonBean
Content Type                :  application/json
Payload(Text Box at bottom) :  { "val1": "Birds", "val2": "Eggs" }

and click on POST button as modifyJson is a POST request type. The response is displayed in the Response window.


Related Articles

Restful WebService using CXF
Restful Client using CXF
SOAP WebService using CXF
SOAP Client using CXF
Restful WebService using Jersey

Recommended Articles

User and Daemon Threads
Thread States
Java 8 Stream
Java 8 Functional Interfaces
Java 8 Lambda Expressions
Top 10 Free Tutorials

Download Source:



Top Picks

1. Top 5 websites offering free e-books

2. Top 10 websites for free Java Articles and Tutorials

3. Top 5 websites for Java Certifications

4. Top 5 Softwares for Java developers

© Copyright 2018. All rights reserved. All trademarks and logos belongs to their owners.         Website Counter