Sunday, September 6, 2015

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 addition program addition.add

//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..");
         // //10 //20 //
         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

 }

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...