Show Topics
Hide Topics

 

So far we used URL directly to Test our RESTful service. But in the real time we will call the services by writing some client application logic.  We have different ways to write a RESTful client

They are

  • net.URL
  • Apache HttpClient
  • RESTEasy client
  • Jersey client

 

Every Java Clinet can send two types of requests

  1. GET
  2. POST

We will see one by one by Example. Here we are Taking JAXRS-JSON-Jersey-Example for writing clients. For all webservices are same. Only difference in Java Clients

 

 

1.Create Dynamic web project in eclipse, convert that into Maven Project

 

2.Configure pom.xml

 

 

3.Configure web.xml

 

 

4.write UserBo POJO class

Write UserBo POJO class & Annotate object with JAXB annotation, for conversion later.

 

 

5.Create Web Service having both @GET @POST for testing with Java Clients

 

 

6.Test Webservice directly by using URL / writing webservice client

 

GET: http://localhost:8080/JAXRS-JSON-JavaClients-Example/rest/json/getjson

POST: http://localhost:8080/JAXRS-JSON-JavaClients-Example/rest/json/postjson

So far we are used above process to Test the Web Services. Now lets see how to test webservices with Java clients.

 

1. java.net.URL

Here we will use “java.net.URL” and “java.net.HttpURLConnection” to create a simple Java client to send “GET” and “POST” request.

 

GET Request Example

 

POST Request Example

 

 

2.Apache HttpClient

Apache HttpClient is available in Maven central repository, just declares it in your Maven pom.xml file.

 

1.Configure POM.xml with Apache HTTPClinet

 

 2.GET Request Example

 

 

3.POST Request Example

 

 

 

 

3.RESTEasy client

 

1.Configure POM.xml with

RESTEasy client framework is included in RESTEasy core module, so, you just need to declares the “resteasy-jaxrs.jar” in your pom.xml file

 

 

2.GET Request Example

 

 

 

3.POST Request Example

 

 

 

4. Jersey client

 

1.Configure POM.xml with

To use Jersey client APIs, declares “jersey-client.jar” in your pom.xml file.

 

 

2.GET Request Example

 

 

3.POST Request Example

 

 

Download Example Code