java2db

How to create a java client to call RESTful web service JAX-RS (Jersey )



Java client is nothing but a simple java class or servlet that calls either java or non-java RESTful web service . The client may request for Html XML JSON or plain text  response from the RESTful web service.Few response types are given below.

 

How to create a java client to call RESTful web service JAX-RS (Jersey )
Example for (media type) Json in RESTful web service ( Jersey ) Basic requirements :

Softwares :
Java (Download).
GlassFish server (Download).
Eclipse IDE (Download).
Jar files:
jersey-client-1.18.jar (Download).
jersey-core-1.18.jar (Download).
jersey-json-1.18.jar (Download).
jersey-server-1.18.jar (Download).
jersey-servlet-1.18.jar (Download).
servlet-api-3.0.jar (Download).
asm-3.1.jar (Download).
jackson-core-asl-1.9.2.jar (Download).
jackson-jaxrs-1.9.2.jar (Download).
jackson-mapper-asl-1.9.2.jar (Download).
jackson-xc-1.9.2.jarjettison-1.1.jar (Download).
jsr311-api-1.1.1.jar (Download).

 

Steps to to create a java client to call RESTful web service :

How to create a java client to call RESTful web service JAX-RS (Jersey ) Step 1 :

Create a normal or dynamic web project in IDE like Eclipse , Netbeans , MyEclipse or RAD.Even we can do it in a stand alone java class.

How to create a java client to call RESTful web service JAX-RS (Jersey )

 

 

How to create a java client to call RESTful web service JAX-RS (Jersey ) Step 2 :

Create a package (com.expert.ws.rest.client.customer) under src folder and create RESfulClient java class.

 

 

 Step 3 :

Add all jar files to WEB-INF/lib folder.

 

 

How to create a java client to call RESTful web service JAX-RS (Jersey ) Step 4 :

Copy the below java code to RESfulClient java class.

 

 

 Example to create a java client to call RESTful web service for text plain response :

First of all , see the RESTful web service  example (Click here for RESTful web service – Text Plain) that receives  text plain request from a client and sends plain text response . In the below example , we are calling it.

 

RESTfulClient.java

Output :
How to create a java client to call RESTful web service JAX-RS (Jersey )

http://localhost:9090/RESTful_PlainText_Response/api/customerInfo/json/1

Java RESTful web service for text plain request and response http:// -: Hiper text transfer protocol

Java RESTful web service for text plain request and response localhost -: Host or IP ( If working with remote PC , use it’s IP )

Java RESTful web service for text plain request and response 9090 -: Eclipse server port (Enter the server port that running in your computer).

Java RESTful web service for text plain request and response RESTful_PlainText_Response -: Project name

Java RESTful web service for text plain request and response api -: configured in web.xml <url-pattern> tag (target)

Java RESTful web service for text plain request and response customerInfo -: @Path(“/customerInfo”) added at the top of CustomerResourceClass.java .

Java RESTful web service for text plain request and response plain -: @Path(“/plain/{customerNo : (\\w+)?}”) added at the top of getCustomerNameJson method .

Java RESTful web service for text plain request and response 1 -: Parameter to the getCustomerNameJson method .

 

 

How to create a java client to call RESTful web service JAX-RS (Jersey ) Example to create a java client to call RESTful web service for XML response :

 

See the RESTful web service  example (Click here for RESTful web service – XML) that receives  XML request from a client and sends XML response . In the below example , we are calling it.

 

RESTfulClient.java

Output :

 

 Example to create a java client to call RESTful web service for JSON  response :

 

First of all , see the RESTful web service  example (Click here for RESTful web service – JSON) that receives  JSON request from a client and sends JSON response . In the below example , we are calling it.

RESTfulClient.java

 

Output :




Related Posts :
Example java RESTful web service for xml response (JAX-RS , Jersey)
Example for (media type) Json in RESTful web service ( Jersey )
Example for (media type) text html in RESTful web service ( Jersey )
Example for (media type) text plain in RESTful web service ( Jersey )
Java RESTful web service example in maven using JAX-RS
RESTful web service example in java using JAX-RS
What is RESTful web service (Introduction to RESTful web services)
   [will not be published]



^ <