Archive

Archive for January, 2013

Java Webservice with Axis

WebServices with Apache Axis:

A)     Creating the WebService Server.

  1. Create a java dynamic web project with eclipse    page1
    2.  Create Java Class, this class will contain a method which converts a given string into upper case string, very small method. We will call this method from the webservice client program.

     package com;
    
    public class TestWebService {
    public String getName(String name){
    return name.toUpperCase();
    }
    }
    

3. Righ click on the class select New -> WebServices  -> WebService

page2

4. Click Next -> Next

page3

5. Click on Start Server leave everything as default and click finish

page4

6. Now check http://localhost:8080/WebServiceTest/services/TestWebService?wsdl in browser

It will give you the WSDL file


<wsdl:definitions targetNamespace="http://com"><!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)--><wsdl:types><schema elementFormDefault="qualified" targetNamespace="http://com"><element name="getName"><complexType><sequence><element name="name" type="xsd:string"/></sequence></complexType></element><element name="getNameResponse"><complexType><sequence><element name="getNameReturn" type="xsd:string"/></sequence></complexType></element></schema></wsdl:types><wsdl:message name="getNameRequest"><wsdl:part element="impl:getName" name="parameters">

</wsdl:part></wsdl:message><wsdl:message name="getNameResponse"><wsdl:part element="impl:getNameResponse" name="parameters">

</wsdl:part></wsdl:message><wsdl:portType name="TestWebService"><wsdl:operation name="getName"><wsdl:input message="impl:getNameRequest" name="getNameRequest">

</wsdl:input><wsdl:output message="impl:getNameResponse" name="getNameResponse">

</wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="TestWebServiceSoapBinding" type="impl:TestWebService"><wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="getName"><wsdlsoap:operation soapAction=""/><wsdl:input name="getNameRequest"><wsdlsoap:body use="literal"/></wsdl:input><wsdl:output name="getNameResponse"><wsdlsoap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="TestWebServiceService"><wsdl:port binding="impl:TestWebServiceSoapBinding" name="TestWebService"><wsdlsoap:address location="http://localhost:8080/WebServiceTest/services/TestWebService"/></wsdl:port></wsdl:service></wsdl:definitions>

B)     Creating the WebService Client

  1. Right click and select New-> WebService->WebServiceClientpage5
    2. Paste the WSDL path in server definition and change the client project to WebServiceTestClient( you can give anything as a name)
    3. Click Next and Finish
    4. It will create a new java dynamic project with named as WebServiceTestClient and some java files

page6

5. Open TestWebServiceProxy.java and write a main() method in that

TestWebServiceProxy.java

public static void main(String... args){
  try{
    TestWebServiceProxy proxy = new TestWebServiceProxy();
    String name = proxy.getName("webservice");
    System.out.println("name -->"+name);
   }catch(RemoteException re){
     re.printStackTrace();
  }
}

C)     Running the client

Right click on TestWebServiceProxy.java and run as java application, it will print the result in Uppercase.

Advertisements
Categories: Java, WebService