Advertisment

Practical guide to building a Web service

author-image
CIOL Bureau
Updated On
New Update

Introduction



The Web Services Development Cycle consists of four main stages: build, deploy, run and manage. The build stage is where the design, development and testing of the web service happens. This stage can be further grouped into four types as

Advertisment



  • Green Field


  • Top Down


  • Bottom Up


  • Meet in the Middle

Using one of these approaches implementations can be provided for developing new breed of web services, transforming existing applications into web service or composing services from existing web services. The web service participants can be classified as Service Requestors, Service Providers or Service Registry based on the role.



During the deploy phase, the web service interface and implementation definitions are published and the run time code for the web service is deployed on a server.



In the run phase the web service is deployed and is operational. The Service Requestor can access the web service by means of Static or Dynamic binding.



The manage phase covers administration of web service and providing QoS for the deployed service like performance, security, scalability etc.



The following sections will give an overview of the approaches to build web services and also look at Static and Dynamic Binding for web services invocation. We will also look at the steps for creating web services by building a sample web service in Java using WebSphere WSDK.



Build Approaches



The first stage in the WSDC is the build. During the build the developer designs and develops the application. The web services model consists of four approaches. These are



  • Green Field




Advertisment

Green Field is used for creating a new web service, where first interfaces are developed from scratch and then the implementation is done. First the web service is created and then Service Interface and Service Implementation definitions are generated from the web service. This approach is used by Service Providers who are building new applications and also want to provide web services functionality for using the same. The Web Service can be implemented in Java, C++, .NET and the WSDL for the web service is generated.





  • Top Down




In this approach, the Service Interface already exists and the web service is developed conforming to the existing interface. The Service Interface is usually a part of an industry standard and Service Providers use this to develop web services. Hence the build starts with the WSDL and from this the web service template can be created in any language like Java, C# etc.



Advertisment



  • Bottom Up




In this approach an existing application is exposed as a web service. The existing application could be an EJB, Java Class or COM application. The Service Interface is derived from this and is made available as a web service. This can be used in Application integration where existing systems need to talk to each other. These systems can be exposed as web services so that different clients can use the Service Implementation to invoke the actual web services. This approach can also be used to link different platforms like VB and J2EE. The J2EE application is exposed as a web service and hence the VB client can talk to it using SOAP.





  • Meet in the Middle




Advertisment

This is used when both the Web Service and Service Interface already exists. The application interfaces need to be mapped to the Service interface that can be done by creating a wrapper for the application that maps to the Service Interface Definition.



Service Requestor



The Service Requestor is the client for the web service and uses the Service Provider. The Service Requestor sends SOAP requests to the Provider and the Service Provider returns back a SOAP response.



Based on the binding the Requestor does with the Provider there are two types



  • Static Binding




Advertisment

Static Binding is built at build time and using the WSDL ( Service Implementation ) of the service the client side stubs are generated. The Service Requestor uses these stubs to call the Service Provider.





  • Dynamic Binding




This type of binding is used when the WSDL (Service Interface and Service Implementation) is not known and the Service Requestor can make a call to the web service at run time.





Continued...

Sample Web Service



The following sections list the steps to be followed for creating a sample web service. WebSphere WSDK is used for building these samples. WSDK contains a servlet/jsp container for hosting web service, AXIS SOAP toolkit for building and deploying web services and a local UDDI registry. You can download WSDK from

www.ibm.com/developerworks .



In the first sample we shall see how to build a green field web service and create the Service Interface and Service Implementations for the same. These are published to the local UDDI registry.



Example 1

(Download the source code here)

Advertisment




About our web service



In this sample we shall create a web service that converts currency from one to another. For example the web service takes Currency from, amount and Currency to as parameters and returns the converted amount. To keep the example simple we shall hardcode the conversion logic in the web service. The following table lists the conversion rates that our web service can support.

Advertisment


INR to USD 47.8



INR to GBP 75.2



INR to SGD 22.3



Creating the web service



  • The first step is to create Java Class with a public method called convert. This method accepts CurrencyFrom, Amount, CurrencyTo as parameters and returns ConvertedAmount as result. The logic for conversion is written in the convert method.


  • Create a web application directory structure called CurrencyConverter in /services/applications. It consists of




  • /services



    /applications



    /CurrencyConverter



    /client



    /deployment



    /webapp



    /META-INF



    /WEB-INF



    /lib



    /classes



    • In the above directory structure the following files are placed



    • /classes - CurrencyConverter.java


    • /client - PublishCurrencyConverter.java, CurrencyConverterClient.java


    • /webapp — wsdl files


    • /deployment - deploy.wsdd


    • /lib - All jars


    • /WEB-INF — web.xml

  • Compile the java source files into the respective directories.


  • The Service Interface and Service Implementation WSDL files are generated from the CurrencyConverter.java using the java2wsdl utility provided by AXIS. ( Use wsdlgen.bat )


  • Create the deployment descriptor ( deploy.wsdd ). This is an XML file and tells axis about the Web Service Deployment.


  • Once all these files are created the web service can be deployed. WSDK provides a GUI utility for Deployment of web service. This utility creates a web application from the files and deploys to the Websphere server.


  • Run the PublishCurrencyConverter to register the interface & implementation in the local UDDI registry.


  • Then run the CurrencyConverterClient to invoke the web service. ( Note : For simplicity the client uses a reference of Service Implementation ) . The client in this example uses Dynamic Binding to call the web service.


  •   The following diagram shows how the sample service works





     

    The code listed in the client is as follows. The client uses a Call to make a web service request. The client uses the WSDL file to create a Service and make a Call. Parts from the WSDL file are highlighted in blue. These include the Implementation WSDL, namespace, service name, port name and operation. This type of invocation is called Dynamic Binding as we use the WSDL to create a service at run time and make the Call.



     



    private static String wsdlServiceImplURL =



    "http://localhost:80/CurrencyConverter/CurrencyConverter_Impl.wsdl";



    String namespace = "http://CurrencyConverter";



    QName serviceQN = new QName(namespace, "CurrencyConverterService");



    Service service = new Service(new URL(wsdlServiceImplURL), serviceQN);



    QName portQN = new QName(namespace, "message");



    call = (Call) service.createCall();



    call.setOperation(portQN, "convert");



    String retVal = (String) call.invoke(new Object<> { "INR", new Double(50000), "SGD" });



    System.out.println("Result :" + retVal);



    CurrencyConverterClient.java



    The Implementation WSDL for the service is listed below. The portions from this that we use in the client are highlighted in blue.





    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"



    xmlns="http://schemas.xmlsoap.org/wsdl/"



    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"



    xmlns:impl="http://CurrencyConverter" xmlns:intf="http://CurrencyConverter-interface"



    xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"



    xmlns:xsd="http://www.w3.org/2001/XMLSchema"



    targetNamespace="http://CurrencyConverter">

















    Implementation WSDL



    The implementation imports the Interface WSDL. This is listed below. We get the operation that we are going to invoke on the web service from the Interface WSDL. This is "convert" and we use this in the CurrencyConverterClient. The Interface also gives details of the request message, response message, porttype and bindings. The SOAP request and response will follow the format specified in the Interface WSDL.





    xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:impl="http://CurrencyConverter"



    xmlns:intf="http://CurrencyConverter-interface"



    xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"



    xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://CurrencyConverter-interface">











































    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://CurrencyConverter-interface"/>









    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://CurrencyConverter-interface"/>











     

    Interface WSDL

    On calling the invoke method from the Client a SOAP request is created and sent to the server. The SOAP request is nothing but an XML file with the body containing the request message. In our case it’s the three parameters that we are sending CurrencyFrom, Amount and CurrencyTo. The SOAP request message is listed below. This message is sent through HTTP and includes HTTP information.





    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"



    xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-



    ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">







    INR



    1000



    USD









    SOAP Request



    The SOAP request is intercepted by the server and it invokes the server class CurrencyConverter that contains the logic for conversion. The response is sent to the client as a SOAP message.







    xmlns:xsd="http://www.w3.org/2001/XMLSchema"



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">







    20.942









     



    SOAP Response



    Before running the client we publish our service to the local UDDI registry. The details we publish include Service Implementation, Service Interface and Service Provider. We use the deploy.wsdd to tell AXIS engine about how to route requests to the web service.



    We can also run our web service without publishing to the UDDI. We only need to tell the axis engine about the web service using the deploy.wsdd. ( Refer Axis Manual )



    Example 2

    (Download the source code here)





    Using a public web service



    In this example we shall consume a public web service whose Service Definitions are registered in a public registry or are present as a wsdl file over the internet..



    About the web service



    The WSDL of the Service is as follows



    http://www.webservicex.net/stockquote.asmx?wsdl



    The above service gives stock quotes for the symbol entered. The Web Service is hosted on a Microsoft platform and we will write a Java client and use Static Binding to access the service and get quotes for stock symbols.



    Creating the Service Requestor



    In this example we only create the Service Requestor as the Service Provider has build and deployed the web service. The different steps are as follows



    • Locate the Service Implementation of the Web Service and get the URL.


    • Using this URL generate the client side classes using wsdl2java tool provided by AXIS.


    • Use these stubs to invoke the web service. This is called static binding as while building the requestor we have the stubs for invocation of the web service. The service can be invoked without the stubs by means of dynamic invocation also.

    The WSDL2java utility generates the following classes



    StockQuoteSoapStub.java



    StockQuote.java



    StockQuoteLocator.java



    StockQuoteSoap.java



    The Client code to invoke the web service is listed below. In case the samples access the internet through a proxy then System properties need to be set in the java client program.



    System.setProperty("proxySet", "true");



    System.getProperties().put( "http.proxyHost", " " );



    System.getProperties().put( "http.proxyPort", " " );





    StockQuote service = new StockQuoteLocator();



    StockQuoteSoap port = service.getStockQuoteSoap();



    System.out.println("Result = " + port.getQuote("IBM"));

     



    Conclusion



    In the preceding sections we have seen how to develop web service from scratch and also use an existing web service. These approaches are used for creating and consuming web services when building applications. In further articles we shall discuss more about building web services and web services composition.



    References



    WebSphere WSDK: http://www-106.ibm.com/developerworks/webservices/wsdk/



    AXIS: http://xml.apache.org/axis/



    Web Services Development Concepts by Peter Brittenham IBM Software Group



    Example1.zip Source Code For Example 1



    Example2.zip Source Code For Example 2



    About the Author:



    The author is working as a Senior Developer in Web Services at MindTree Consulting Pvt. Ltd. He can be reached at krishnakumarb@mindtree.com

    tech-news