Thursday, December 30, 2010

Getting started with Apache Axis2

I believe my previous post helped you to setup Apache Axis2 on windows. Now let's have a look at how to host a simple Web service with Apache Axis2.


Start an eclipse project and write a simple java class which will output a string "Hello!!!".

Sample1.java

package webservices.samples;

public class Sample1 {

 public String sayHello(String name) {

  return name;

 }

}


Inside src folder of your eclipse project create a new folder called META-INF and create a new file called services.xml in it.





 
  
 
 webservices.samples.Sample1
 
 


The service element has information about one service, inside service element specify the full qualified class name of your webservice (in this case it is webservices.samples.Sample1). In the child element of service element set the attribute name to the operation name you need to invoke through webservice (in this case it is sayHello)


Using Eclipse build the project and export only the relevant package and the META-INF inside bin folder as an archive file(.aar) to the repository\services folder inside axis2 home folder.


Start Axis2 server by double clicking on axis2server.bat inside bin folder. Now it will show deploying your service.


Now fire up a web browser and paste the following address in address bar http://localhost:8080/axis2/services/. It will list down the deployed services. Now your service should also be in that list.


Generating client to access the service

Goto bin folder inside axis2 home folder using command prompt and run the following command.

wsdl2Java.bat -uri http://localhost:8080/axis2/services/mywebservice?wsdl -o C:\myfile -uw

now go to C:\myfile folder using command prompt and give the following command

ant

Make sure you have set Apache ant bin folder to environment variable path before using this command. It will create a folder called build inside it there will be a folder called lib and mywebservice-test-client.jar will be generated inside it. You must use mywebservice-test-client.jar as an external jar file in your client project. Not only that jar you must add another set of external jar files in to your client project. All those jar files reside in lib folder inside axis2 home. Here I have list them out.


axis2-kernel-1.5.jar
axiom-api-1.2.8.jar
axiom-dom-1.2.8.jar
axiom-impl-1.2.8.jar
commons-logging-1.1.1.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.3.jar
neethi-2.0.4.jar
axis2-transport-local-1.5.jar
axis2-transport-http-1.5.jar
commons-httpclient-3.1.jar
mail-1.4.jar
commons-fileupload-1.2.jar
woden-api-1.0M8.jar
woden-impl-dom-1.0M8.jar
httpcore-4.0.jar
commons-codec-1.3.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
wstx-asl-3.2.4.jar
axis2-jaxws-1.5.jar
axis2-adb-1.5.4.jar

Now you can write the client code. Build the project and run it. Make sure you have started axis2 server when you run the client.


Cilent.java
package webclient.samples;

import webservices.samples.MywebserviceStub;

public class Client {
 
 public static void main(String args[]){

 try {
 MywebserviceStub stub =new MywebserviceStub("http://localhost:8080/axis2/services/mywebservice");
 System.out.println(stub.sayHello("Hello"));
 }catch (Exception e){
 }
 
 }

}


Note: in axis2 home inside conf folder there is axis2.xml file open it and set hotupdate="true", this will auto update axis2 service whenever you update an already deployed service without restarting the axis2 service.

No comments :

Post a Comment