Labels

Saturday, June 23, 2012

How to deploying and accessing a simple web service using Apache Axis

The steps we will walkthrough

Code – code a simple HelloWorld java class that we want to expose as web service

Java2WSDL – Generate  the WSDL file for the given HelloWorld Interface

WSDL2Java – Generate the Server side wrapper class and stubs for easy client access

Deploy – deploy the service to apache axis

Client – code  a simple client that access our HelloWorld Web Service

Step 1: Code
HelloWorld.java

package helloworld;

public interface HelloWorld {
     public String sayHello( String name);
}


Compile (Cont)

HelloWorldImpl.java

package helloworld;

public class HelloWorldImpl implements HelloWorld {
    
    public String sayHello( String name){
        if(name == null)
            return “Hello Everyone”;
        else
            return “Hello “ + name;
    }

}




Step 2: Java2WSDL



This command will generate the WSDL that
Conforms to our interface
% java org.apache.axis.wsdl.Java2WSDL
         -o hello.wsdl
         -l http://localhost:8080/axis/services/helloworld
              -n urn:helloworld
               -p“helloworld" urn:helloworld
               helloworld.HelloWorld
Parameters description
    -o = Name of the output
     -l  = URL of the web Service
     -n = Target Namespace for the WSDL
     -p = Map Java package to namespace   
     Fully Qualified Class Name



Step 3: WSDL2Java



This command will generate the wrapper code for
deploying the service, as well as client stubs for
accessing it.
% java org.apache.axis.wsdl.WSDL2Java
         -o .
         -d Session
              -s
               -p helloworld.gen
               hello.wsdl
Parameters description
    -o = Base output Directory
     -d  = Scope of deployment
     -s = To generate Server-side code too
     -p = Package to place the code
     Name of the WSDL




These are the codes that will get generated

HelloWorldSoapBindingImpl.java – This is the implementation code for the Web Service

HelloWorld.java – This is the remote interface

HelloWorldService.java – This is the service interface

HelloWorldServiceLocator.java – Helper class to retrieve handler to service

HelloWorldSoapBindingSkeleton.java – Server-side skeleton code

HelloWorldSoapBindingStub.java – Client side stub

deploy.wsdd – axis deployment descriptor

undeploy.wsdd – deployment descriptor to undeploy the web services from the Axis System


WSDL2Java


HelloWorldSoapBindingImpl
package helloworld.gen;

public class HelloWorldSoapBindingImpl implements helloworld.gen.HelloWorld
{
   
   
    public String sayHello(String str0) throws             java.rmi.RemoteException {
       
    }
}



Step 4: Deploy



Compile the Service Code
% javac helloworld\gen\*.java

Package the code for Axis
% jar cvf hello.jar helloworld/*.class helloworld/gen/*.class
% mv hello.jar $TOMCAT_HOME/webapps/axis/WEB-INF/lib

Deploy the Service using WSDD
% java org.apache.axis.client.AdminClient deploy.wsdd
<admin> Done processing </Done>

Step 5: Client



package helloworld;

Import helloworld.gen.*;

public class HelloWorldTester {
   
    public static void main(String [] args)
            throws Exception
    {
        // Make a service
        HelloWorldService service = new                                                                  HelloWorldServiceLocator();

        // Now use the service to get a stub to the service
        helloworld.gen.HelloWorld hello = service.getHelloWorld();

        // Make the actual call    
        System.out.println( hello.sayHello(“Java Gurus”) );
    }
}



key features of Axis :


Speed. Axis uses SAX (event-based) parsing to acheive significantly greater speed than earlier versions of Apache SOAP.

Flexibility. The Axis architecture gives the developer complete freedom to insert extensions into the engine for custom header processing, system management, or anything else you can imagine.

 Stability. Axis defines a set of published interfaces which change relatively slowly compared to the rest of Axis.

WSDL support. Axis supports the Web Service Description Language, version 1.1, which allows you to easily build stubs to access remote services, and also to automatically export machine-readable descriptions of your deployed services from Axis.

Component-oriented deployment. You can easily define reusable networks of Handlers to implement common patterns of processing for your applications, or to distribute to partners.

Transport framework. We have a clean and simple abstraction for designing transports
(i.e., senders and listeners for SOAP over various protocols such as SMTP, s-oriented middleware, etc),
   and the core of the engine is completely transport-independent.

No comments:

Post a Comment