Thursday, 22 March 2012

apache axis web service - tutorial 1





//add.java


package addition;
public class add{
public int add(int a,int b){
return a+b;
}
}

//inside your service package place that add.class file
//services dir -addition dir-add.class



//services.xml


<?xml version="1.0" encoding="UTF-8" ?>
<service name="arithmatic">
<description>addition program </description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">addition.add</parameter>
</service>


//services dir--addition dir--META-INF dir- services.xml

//test the your web services
http://localhost:8080/axis2/services/arithmatic

//the previous links show wsdl file means your service working file...

//if it show fault services means your services have some problem..

//the error may be services.xml syntax problem(so careful in writing services.xml)


//client program


import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.om.OMText;


import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.addressing.EndpointReference;


public class client{

public static void main(String args[]) throws Exception{

System.out.println("client program..");


//<add>
//<a>10</a>
//<b>20</b>
//</add>


OMFactory fac= OMAbstractFactory.getOMFactory();

OMNamespace omns=fac.createOMNamespace("http://addition","arith1");

OMElement add=fac.createOMElement("add",omns);


//create a xml request element with child element 'a' and 'b'

OMElement a=fac.createOMElement("a",omns);
a.setText("10");
add.addChild(a);

OMElement b=fac.createOMElement("b",omns);
b.setText("20");
add.addChild(b);

System.out.println("xml request: " + add );


EndpointReference epr=new EndpointReference("http://localhost:8080/axis2/services/arithmatic?wsdl");

Options op= new Options();

op.setTo(epr);

ServiceClient sc=new ServiceClient(); //using this class invoke web service

sc.setOptions(op);

OMElement result=sc.sendReceive(add); //this method for request and response

System.out.println("service response: "+ result); //print the response xml in console

}

}



//complile the java program with apache axis package as class path otherwise you get class definions not found error

ex: javac cp "c:/tomcat/webapps/axis2/services/webinf/lib/*;" client.java

//execute the java program with apache axis package as class path

ex: java cp "c:/tomcat/webapps/axis2/services/webinf/lib/*;" client



OMElement methods();


string getText(); //retrive the element value

setText(string); //set the element value

getFirstElement(); //retrive the first child element



OMFactory methods();


OMNamespace createOMNamespace("URI","namespace");

OMElement createOMElement("element name",namespace);

OMText createOMText("text value");



Options method();


setTo(EndpointReference);// set the target web service address using endpoint reference class


ServiceClient methods();

setOptions(OPTIONS);

OMElement send(OMElement); //for sending oly

 OMElement SendReceive(OMElement); //for sending receving xml request and response





2 comments: