Tuesday, November 16, 2010

Create a Hello World Webservice Using Axis2


Steps involved in creating Hello World Webservice using axis2
Steps :
1.       Develop a service class(the one you are going to expose as a service)
2.       Write a service descriptor e.g  services.xml
3.       Compile the service class and create web services archieve file
4.       Deploy into axis server(copy the aar file into services folder).


Step 1:      Create the Service class


Package vels.test.ws;

public class HelloWorldService 
{
  public String sayHello(String name) {
    
      out.println("Hello World Service called");
    return "Hello : " + name;
  }
}


Step 2 : Write a Webservice descripttor (service configuration file)

In Axis2 the service configuration (descriptor) file used is services.xml.
The services.xml file must present in the META-INF directory of the service achieve.

services.xml  :
  <service>
 <parameter name="ServiceClass" locked="false">  
                      vels.test.ws.HelloWorldService</parameter>
 <operation name="sayHello">
 <messageReceiver 
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>


                 Here  our  service class is vels.test.ws.HelloWorldService  and
                 the message receiver is org.apache.axis2.rpc.receivers.RPCMessageReceiver.
                 The operation we are exposing is sayHello.

Step 3 :  Creating the archive file for the deployment


  •   Compile all the java files and make it as jar(aar)
  •     For example you have placed all the class files into the following  folder D:\dev\HelloWorld\bin
  •    Run the following command(before that set the java class path )D:\dev\HelloWorld\bin>jar cvf HelloWorldService.aar *(it will take all  the class files and services.xml files and make it as HelloWorldService.aar file)

Deploy the Webservice :    


Creating Client program to access the deployed webservice :

1.   Generate the client side java class From WSDL using axis  WSDLToJava     This will create client specific java files
      
        Place all these jars into class path
        set CLASSPATH=.;D:\lib\axis\axis-1.3.jar;
                   D:\lib\axis\commons-logging-1.0.4.jar;
                   D:\lib\axis\commons-discovery-0.2.jar;
                   D:\lib\axis\jaxrpc.jar;
                   D:\lib\axis\log4j-1.2.8.jar;
                   D:\lib\axis\wsdl4j-1.5.1.jar;D:\lib\axis\saaj.jar;

     java org.apache.axis.wsdl.WSDL2Java ..\res\wsdl\Hello.wsdl -o ..\src –W


2. write a client to test the service

public class Test {
    public static void main(String[] args) throws Exception {
      HelloSOAP11BindingStub stub = new HelloSOAP11BindingStub(new java.net.URL("http://localhost:8080/axis2/services/hello"),null);
        //Create the request
       
       SayHello  request = new
  SayHello();

       request.setName("vels");
        //Invoke the service
        SayHelloResponse response
           = stub.sayHello(request);
       System.out.println("Response : " + response.get_return());
   }
}


Hope u guys got come idea abt webservices.........

1 comment: