webservice之axis2方式开发总结

webservice开发总结

首先建一个HelloWorld的web工程

把axis2相关包放到工程lib里,

为保证正确,新手建议引用所有axis2的jar包.

===================================

1.开发service端

HelloWorld.java

package sample;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;  
import org.apache.axiom.om.OMFactory;  
import org.apache.axiom.om.OMNamespace;  

public class HelloWorld {
       //读取client端getSayHelloOMElement()方法传递的参数in
       public OMElement sayHello(OMElement in){
             //将in转换为String
              String name = in.getText();
              String info = name + "HelloWorld!";
              //创建response SOAP包
              OMFactory fac=OMAbstractFactory.getOMFactory();
              // OMNamespace指定此SOAP文档名称空间
              OMNamespace omNs=fac.createOMNamespace("http://www.helloworld.com/","");
               //创建元素sayHello,并指定其在omNs指代的名称空间中
              OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
              //指定元素的文本内容
              resp.setText(info);
              return resp;
       }
}



=====================================

2.service.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorld">
<description>
This is a sample Web Service.
</description>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>



======================================

3.webservice客户端开发

package example.client;

import java.util.Iterator;

import org.apache.axiom.om.OMAbstractFactory;  
import org.apache.axiom.om.OMElement;  
import org.apache.axiom.om.OMFactory;  
import org.apache.axiom.om.OMNamespace;  
import org.apache.axis2.addressing.EndpointReference;  
import org.apache.axis2.client.Options;  
import org.apache.axis2.client.ServiceClient;  
   
public class TestClient {
       private static EndpointReference targetEPR=new EndpointReference
         ("http://localhost:8080/axis2/services/HelloWorld");
       public static OMElement getSayHelloOMElement(){
              //创建request SOAP包
              OMFactory fac = OMAbstractFactory.getOMFactory();
              // OMNamespace指定此SOAP文档名称空间
              OMNamespace omNs = fac.createOMNamespace("aa","");
              //创建元素sayHello,并指定其在omNs指代的名称空间中
              OMElement method = fac.createOMElement("sayHello",omNs);
              //指定元素的文本内容
              method.setText("Sailing ");
              return method;
       }
       public static void main(String[] args){
              try{
                     Options options = new Options();
                     options.setTo(targetEPR);
                     ServiceClient sender = new ServiceClient();
                     sender.setOptions(options);
                     OMElement sayHello = TestClient.getSayHelloOMElement();
                    //发出request SOAP
                    //同时将得到的远端由sayHello方法返回的信息保存到result
                     //通过services.xml能准确找到sayHello方法所在的文件
                     OMElement result = sender.sendReceive(sayHello);
                     //OMElement tempOm;
                    
                     System.out.println(result.equals(null));
                     System.out.println(result.getText());
                     System.out.println(result.getLocalName().intern());
//                     for (Iterator it = result.getFirstElement().getChildElements(); it.hasNext();){
//             tempOm = (OMElement) it.next();
//             System.out.println(tempOm.getLocalName());
//             }
              }
              catch(Exception axisFault){
                     axisFault.printStackTrace();
              }
       }
}

============================================

4.服务端工程部署

服务端工程部署有两种方式:arr包和非aar包

服务端工程部署在 %TOMCAT_HOME%/webapps\axis2\WEB-INF\services

1)非aar包方式:

也就是webservice工程模式,其工程结构如下:

%TOMCAT_HOME%/webapps\axis2\WEB-INF\services\HelloWorld\sample\HelloWorld.class

%TOMCAT_HOME%/webapps\axis2\WEB-INF\services\HelloWorld\META-INF\MANIFEST.MF

%TOMCAT_HOME%/webapps\axis2\WEB-INF\services\HelloWorld\META-INF\services.xml

2)aar包方式:

HelloWorld文件夹打包成HelloWorld.aar放到%TOMCAT_HOME%/webapps\axis2\WEB-INF\services目录下



=============================================

5.启动Tomcat

%TOMCAT_HOME%/bin/startup.bat



=============================================

6.测试客户端

运行TestClient.java main()

如果显示:Sailing HelloWorld!,那就说明测试成功.
posted @ 2008-11-04 09:33  Earl_86  阅读(1883)  评论(0编辑  收藏  举报