Thursday, 16 April 2015

Creating a web service


In this tutorial, I am going to explain how to create a webservice using eclipse.

There are two basic approaches to create web service from scratch.
a.   Bottom up Approach
b.   Top down Approach

Bottom up Approach
In bottom up approach, first you implement the service. Then the XML description (WSDL) of the service is produced and published in UDDI.


Universal Description, Discovery and Integration (UDDI) is a directory service where businesses can register and search for Web services.

Web Services Description Language (WSDL) is a format for describing a Web Services interface. It is a way to describe services and how they should be bound to specific network addresses.

Top Down Approach
This approach starts with the XML description of the service before it is implemented. This (WSDL) specification is then used as a guide to writing the code that implements the service.

To work on this tutorial, you must install eclipse and tomcat. The web service, I am going to demonstrate is pretty straight forward, it takes Principal (P), Time (T), Rate or interest (R) as input and gives you the Interest (I) by using the following formula.

I = PTR/100
Where I = Interest, P = Principal, T = Time and R = Rate or interest.

Step 1: Create a dynamic web project in eclipse, which will host your web service.


Give the project name as “InterestCalculator” and press finish.

Step 2: Create a package “tutorial.webservices.services”.

Step 3: Create InterestCalc.java in the package “tutorial.webservices.services”.

InterestCalc.java

package tutorial.webservices.services;

public class InterestCalc {
 public double getInterest(double principal, int time, double rateOfInterest) {
  return (principal * time * rateOfInterest)/100;
 }
}


Total Project structure looks like below.


Step 4: Generate web service and client.
Till now, we create a project with simple code to calculate interest. Eclipse will do the rest for you to make webservice.

Select the InterestCalc.java' class in the Project Explorer and go to File > New > Other, then open the 'Web Services' folder and select 'Web Service', then click 'Next'.




The next dialogue box has three sections.


The upper section relates to the web service that you are creating. It shows web service type as "Bottom up Java Bean Web Service" which indicates that you are producing a web service from the bottom up based on some Java code (from InterestCalc.java). The slider control at the left side set to "Start service".

The middle section allows you to specify that a client should be generated so that you can test the web service. Use the slider control to generate Test client. The slider control for the client should be set to the 'Test client' level, so that Eclipse generates a client with which you can test the service

The lower section contains two check boxes, which allow you to select whether the web service is to be published (to UDDI) or monitored. Before clicking 'Finish' tick the 'Monitor the Web service' checkbox. This will instruct Eclipse to start the TCP/IP Monitor, which will display the messages that are sent between Eclipse (via the proxy) and the web service. When you test the service you will then be able to view these XML messages.

Final window looks like below.


Click 'Finish', Eclipse will start to create and deploy the web service and make the client for testing the service.

When eclipse, finished with producing webservice and client, you can able to see two new items added to the project explorer.


The first is a Java project called “InterestCalculatorClient”, which Eclipse created to hold the code for the client that has been generated.

The second is a new folder called 'JSR-109 Web Services'. This folder is intended as the location for any files generated that support the Java Specification Request (JSR) 109.

Eclipse also opens a web browser with a tab named Web Services Test Client. The web browser displays the client web pages as a frameset of three pages with headings 'Methods', 'Inputs' and 'Result'.

Step 5: Testing the web service using the client
You can test the web service now using the Methods pane. Click on the method getInterest in methods pane.


Enter inputs, and press Invoke, you will get the result in “Result” pane.


Monitoring web service messages
At the bottom of the Eclipse window, you will see that both the request put to the service and the response from the service are displayed in the TCP/IP Monitor. Select Request viewer type as “xml” to see the request and responses in xml format.

Request put to the service like below.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getInterest xmlns="http://services.webservices.tutorial">
<principal>6500.0</principal>
<time>5</time>
<rateOfInterest>2.0</rateOfInterest>
</getInterest>
</soapenv:Body>
</soapenv:Envelope>


Response from the service like below.

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getInterestResponse xmlns="http://services.webservices.tutorial">
<getInterestReturn>650.0</getInterestReturn>
</getInterestResponse>
</soapenv:Body>
</soapenv:Envelope>


Generate Web service client from wsdl file
Use the follow url to get wsdl file locations.


 
Following is the wsdl location for InterestCalc


You can see the wsdl location in project explorer also.

 
Step 1: Create stub for the web service.

A stub for a remote object acts as a client's local representative or proxy for the remote object. The caller invokes a method on the local stub, which is responsible for carrying out the method call on the remote object.

When a stub's method is invoked, it does the following:
a. Initiates a connection with the remote JVM containing the remote object,
b. Marshals (writes and transmits) the parameters to the remote JVM,
c. Waits for the result of the method invocation,
d. Unmarshals (reads) the return value or exception returned, and returns the value to the caller.

We can create stub by using “wsimport” command and WSDL file, provided by the web service provider. “wsimport” comes with java SE installation itself.

open the terminal,
a. Create  a directory “src” to store all the java files generated by wsimport.

$ pwd
/Users/harikrishna_gurram/study/webservices/examples
$ mkdir src
$ ls
src


b. Run wsimport command like below.

$ wsimport -keep -s src http://localhost:8080/InterestCalculator/services/InterestCalc?wsdl
parsing WSDL...


generating code...


compiling code...
 
It creates all the java sources files for the web service and compile and place the class files in proper directory structure.

$ pwd
/Users/harikrishna_gurram/study/webservices/examples
$ ls
src  tutorial
$ cd src
$ ls
tutorial
$ cd tutorial/
$ ls
webservices
$ cd webservices/
$ ls
services
$ cd services/
$ ls
GetInterest.java  InterestCalc.java  ObjectFactory.java
GetInterestResponse.java InterestCalcService.java package-info.java


Step 2: Create new java project “InterestCalcClient” in eclipse. Create a package “tutorial.webservices.services”.

Copy all the files generated in step1 into the package “tutorial.webservices.services”.

Step 3: Create tutorial.test package, and create “MyClient.java” in the package tutorial.test.

package tutorial.test;

import tutorial.webservices.services.InterestCalcService;
import tutorial.webservices.services.InterestCalc;

public class MyClient {
 public static void main(String args[]){
  InterestCalcService service = new InterestCalcService();
  InterestCalc calc = service.getInterestCalc();
  System.out.println(calc.getInterest(6500, 5, 2));
 }
}


Run MyClient.java, you will get output 650.0. Total project structure looks like below.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment