Friday, March 13, 2015

Web services - Part4


Creating SOAP Web Service
We will create a simple web service using eclipse, Apache Tomcat Server 5.5, Apache Axis web service runtime using “Bottom up Java Bean Web Service”
1.        Create New Dynamic Project



2.       Create new class that will be used to create Web service in Bottom up Approach ( that is using this class eclipse will generate WSDL etc )
package com.kmingle.webservices.soap;

public class HelloWebserviceClass {

       public String sayHello(String name){
              return "Hello, from "+name;
       }
}

3.        Right Click on Dynamic Web Project and select new > Web Service
Here simultaneously you can create test client also, to test the Web service.



4.        Start the server


5.        Use following link in browser to test newly created Web service



It will show following SOAP request and response in TCP/IP Monitor view ( if you have selected “Monitor the web service” in step 3 )


SOAP Request:
  
      satyendra
  

SOAP Response:
  
      hello, from satyendra
  

Further, you can explore the generate WSDL file under /WebContent/wsdl path. Below is the generate WSDL (HelloWebServiceClass.wsdl )

xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://soap.webservices.kmingle.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://soap.webservices.kmingle.com" xmlns:intf="http://soap.webservices.kmingle.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://soap.webservices.kmingle.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="sayHello">
    <complexType>
     <sequence>
      <element name="name" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="sayHelloResponse">
    <complexType>
     <sequence>
      <element name="sayHelloReturn" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
  </schema>
 </wsdl:types>

   <wsdl:message name="sayHelloResponse">

      <wsdl:part element="impl:sayHelloResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="sayHelloRequest">

      <wsdl:part element="impl:sayHello" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="HelloWebserviceClass">

      <wsdl:operation name="sayHello">

         <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest">

       </wsdl:input>

         <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="HelloWebserviceClassSoapBinding" type="impl:HelloWebserviceClass">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="sayHello">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="sayHelloRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="sayHelloResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="HelloWebserviceClassService">

      <wsdl:port binding="impl:HelloWebserviceClassSoapBinding" name="HelloWebserviceClass">

         <wsdlsoap:address location="http://localhost:8080/ExampleWebProject/services/HelloWebserviceClass"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>


So we have seen that creating a soap web service using tools like eclipse is much simpler than understanding its technology stacks :-) .


In my next blog we will see Implementing RESTful web service


< Types of Web services                    How to create and consume RESTful web service >                  

1 comment:

Sanjay Pani said...

Nice Explanation with simple steps.. waiting for the restful blogs..

Total Pageviews