This article is for the beginners and people who are new to Web Services. I have compiled the things you need to know when you get started with Web Service. Screenshots are available in the attached document.
Web Services Introduction:
Web Services help in exposing the available services in your application to the Web thereby making your application a web application. With web services exchange of data between different applications on different platforms becomes simple. Web Services are application components that communicate using XML + HTTP. Web Services use XML to encode and decode data. It uses SOAP to transport the data.
It is composed of three elements
• SOAP – Simple Object Access Protocol (Platform independent, XML Based)
• UDDI – Universal Description, Discovery and Integration
• WSDL – Web Services Description Language
SOAP – Platform independent, XML based protocol for accessing a web service.
UDDI - A directory service where companies can register and search for Web services.
WSDL - XML based language that describes Web Services message formats and protocols.
Creating Web Services:
Web Services can be created in two ways.
• Top Down Approach (WSDL Coding)
• Bottom Up Approach (Coding WSDL)
Top down Approach:
In this method, the WSDL is created first using the WSDL editor. The web service wizard on eclipse can be used to create skeleton Java Classes and the desired implementation can be done on or based out of those classes. Creating the WSDL first gives more control over the web service and hence this is the recommended way of implementing a web service.
Bottom up Approach:
In this method, the desired implementation is done through coding and the service class (Java Bean or EJB) is exposed as Web Service. Web Service wizard in eclipse can help in creating the WSDL file and web service configurations corresponding to the Service class that is exposed. This method is easier and faster and used by developers who are new to Web Service.
IDE / JDK requirement:
- Get Eclipse 3.2 or above.
- Install JDK 1.4 or above.
- Install Tomcat 5.5 or above.
The example project here uses Eclipse 36 (Helios, Java EE IDE for Web Developers), JDK 1.6 and Tomcat 6.0.1.2
Getting Started with Web Service:
Let’s build a web service that accepts a name and says Hello. i.e. If you pass “John”, the web service will return a string “Hello John” as response. The method exposed to web here is “sayHello”.
Top Down Approach:
Create a Workspace with a dynamic web project (2.5).
Copy axis.jar, saaj.jar, jaxrpc.jar to the lib folder of WEB-INF.
Create a folder named “wsdl” inside your WEB-INF. Copy/Paste your wsdl file to that.
Right Click on the WSDL file (Hello.wsdl), Select Web Services and Create Java Bean Skeleton.
Web Service Wizard will pop up. Carefully note down the text fields and values. Do not change anything here. Click Next.
Click Next
Now your Java Bean classes are created. Click on Start Server. This will add the Web Project (Hello_Web) to the Tomcat server and start the server. You can also see the Web Service Descriptor files created inside Web Project (deploy.wsdd and undeploy.wsdd).
Bottom Up Approach:
Create a new dynamic web project (Hello_WebWS) .
Copy axis.jar, saaj.jar, jaxrpc.jar to the lib folder inside WEB-INF.
Create a Java Class (com.webservice.sample.Hello.java) with a public method with signature “public String sayHello (String name)”. Let the method return the string “Hello + name”.
Right Click on Hello.java Web Services Create Web Service
Web Service Wizard will show up with Web Service type as “Bottom Up” Approach. No changes here. Click Next.
All your public methods in Hello.java should come here. Select the methods you want to be exposed as Web Service. Click Next.
You do not want to publish your web service now. So, click finish button here.
Your WSDL file, Web Service Descriptors (deploy.wsdd, undeploy.wsdd) are created on Web Project, and web project added to server and should be in STARTED status now.
Testing Web Services:
Testing the web service is easy and is same for both the development approaches. Right click on the WSDL, Select Test with Web Services Explorer.
Click on the sayHello method. It will show a text field to accept the input (name). Type in “John” and click on GO. You will see the response printed on the explorer.
Hope it helps... :)
No comments:
Post a Comment