`

详解Axis2实现Web Services之ADB篇

    博客分类:
  • J2EE
阅读更多

构建一个新的Web Services服务,会有很多种不同的方法,你即可以用pojo结合rpc模式来写,也可以用axis2自己的axiom api从底层写,也可以从wsdl生成相应框架,然后填写相应逻辑。

对于wsdl生成框架代码这种形式,又有不同的数据绑定可以使用。axis2 1.1 可以使用adb(axis2自己的data binding)、xmlbeans、jibx、jaxme、jaxbri。其中常用的还是前两种,axis2官方是极力推荐自己的data binding的,因为速度快,架构相对简单,而且有一些独有的特性。xmlbeans,是对模式支持最好的data binding,一些特别复杂的模式只能在xmlbeans下才能正确生成框架代码。其他几种代码wsdl2java目前都不是支持得很好,都还在实验改进阶段。

面对这么多数据绑定模式,很多人都无从选择。官方maillist上也有好多人问该用哪个。查了一些资料,据说,adb很好用,而且速度快,如果只是简单的模式,那么使用这种数据绑定就可以了。xmlbeans的强力模式支持,可以支持足够负责的模式,但是速度上也只是比adb低5%左右。

axis2的wsdl2java工具对于adb以及xmlbeans支持都已经很好了,可以直接从wsdl2java生成相应的绑定类

Axis2 还提供自己的简单数据绑定框架,称为 Axis2 Data Binding (ADB)。这就允许用户下载 Axis2 并使用它,而不用太多考虑数据绑定框架。由于 ADB 和 Axis2 内部元素之间实现了紧密的集成,可以预见,与其他框架相比,此框架的性能和生成代码维护方面都更具优势。
 
为了充分利用资源,我们用上一个有关AXIOM专题的例子中生成的WSDL文件来为这个例子服务

1.生成ADB服务框架。使用的WSDL文件如下:

xml 代码

 

  1. <wsdl:definitions xmlns:axis2="http://service.util.danlley.org" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"  
  2. xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"  
  3. xmlns:ns="http://service.util.danlley.org/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"  
  4.  targetNamespace="http://service.util.danlley.org">  
  5.  <wsdl:types>  
  6.   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified"  
  7.    targetNamespace="http://service.util.danlley.org/xsd">  
  8.    <xs:element name="getHelloMessageFault">  
  9.     <xs:complexType>  
  10.      <xs:sequence>  
  11.       <xs:element name="getHelloMessageFault" type="xs:anyType" />  
  12.      xs:sequence>  
  13.     xs:complexType>  
  14.    xs:element>  
  15.    <xs:element name="getHelloMessage">  
  16.     <xs:complexType>  
  17.      <xs:sequence>  
  18.       <xs:element name="element" nillable="true" type="xs:anyType" />  
  19.      xs:sequence>  
  20.     xs:complexType>  
  21.    xs:element>  
  22.    <xs:element name="getHelloMessageResponse">  
  23.     <xs:complexType>  
  24.      <xs:sequence>  
  25.       <xs:element name="return" nillable="true" type="xs:anyType" />  
  26.      xs:sequence>  
  27.     xs:complexType>  
  28.    xs:element>  
  29.    <xs:element name="getMoreMessageFault">  
  30.     <xs:complexType>  
  31.      <xs:sequence>  
  32.       <xs:element name="getMoreMessageFault" type="xs:anyType" />  
  33.      xs:sequence>  
  34.     xs:complexType>  
  35.    xs:element>  
  36.    <xs:element name="getMoreMessage">  
  37.     <xs:complexType>  
  38.      <xs:sequence>  
  39.       <xs:element name="element" nillable="true" type="xs:anyType" />  
  40.      xs:sequence>  
  41.     xs:complexType>  
  42.    xs:element>  
  43.    <xs:element name="getMoreMessageResponse">  
  44.     <xs:complexType>  
  45.      <xs:sequence>  
  46.       <xs:element name="return" nillable="true" type="xs:anyType" />  
  47.      xs:sequence>  
  48.     xs:complexType>  
  49.    xs:element>  
  50.   xs:schema>  
  51.  wsdl:types>  
  52.  <wsdl:message name="getHelloMessageMessage">  
  53.   <wsdl:part name="part1" element="ns:getHelloMessage" />  
  54.  wsdl:message>  
  55.  <wsdl:message name="getHelloMessageResponseMessage">  
  56.   <wsdl:part name="part1" element="ns:getHelloMessageResponse" />  
  57.  wsdl:message>  
  58.  <wsdl:message name="getHelloMessageFault">  
  59.   <wsdl:part name="part1" element="ns:getHelloMessageFault" />  
  60.  wsdl:message>  
  61.  <wsdl:message name="getMoreMessageMessage">  
  62.   <wsdl:part name="part1" element="ns:getMoreMessage" />  
  63.  wsdl:message>  
  64.  <wsdl:message name="getMoreMessageResponseMessage">  
  65.   <wsdl:part name="part1" element="ns:getMoreMessageResponse" />  
  66.  wsdl:message>  
  67.  <wsdl:message name="getMoreMessageFault">  
  68.   <wsdl:part name="part1" element="ns:getMoreMessageFault" />  
  69.  wsdl:message>  
  70.  <wsdl:portType name="HelloWorldAxiomPortType">  
  71.   <wsdl:operation name="getHelloMessage">  
  72.    <wsdl:input message="axis2:getHelloMessageMessage" />  
  73.    <wsdl:output message="axis2:getHelloMessageResponseMessage" />  
  74.    <wsdl:fault message="axis2:getHelloMessageFault" name="getHelloMessageFault" />  
  75.   wsdl:operation>  
  76.   <wsdl:operation name="getMoreMessage">  
  77.    <wsdl:input message="axis2:getMoreMessageMessage" />  
  78.    <wsdl:output message="axis2:getMoreMessageResponseMessage" />  
  79.    <wsdl:fault message="axis2:getMoreMessageFault" name="getMoreMessageFault" />  
  80.   wsdl:operation>  
  81.  wsdl:portType>  
  82.  <wsdl:binding name="HelloWorldAxiomSOAP11Binding" type="axis2:HelloWorldAxiomPortType">  
  83.   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />  
  84.   <wsdl:operation name="getHelloMessage">  
  85.    <soap:operation soapAction="urn:getHelloMessage" style="document" />  
  86.    <wsdl:input>  
  87.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  88.    wsdl:input>  
  89.    <wsdl:output>  
  90.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  91.    wsdl:output>  
  92.    <wsdl:fault name="getHelloMessageFault">  
  93.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  94.    wsdl:fault>  
  95.   wsdl:operation>  
  96.   <wsdl:operation name="getMoreMessage">  
  97.    <soap:operation soapAction="urn:getMoreMessage" style="document" />  
  98.    <wsdl:input>  
  99.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  100.    wsdl:input>  
  101.    <wsdl:output>  
  102.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  103.    wsdl:output>  
  104.    <wsdl:fault name="getMoreMessageFault">  
  105.     <soap:body use="literal" namespace="http://service.util.danlley.org" />  
  106.    wsdl:fault>  
  107.   wsdl:operation>  
  108.  wsdl:binding>  
  109.  <wsdl:binding name="HelloWorldAxiomSOAP12Binding" type="axis2:HelloWorldAxiomPortType">  
  110.   <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />  
  111.   <wsdl:operation name="getHelloMessage">  
  112.    <soap12:operation soapAction="urn:getHelloMessage" style="document" />  
  113.    <wsdl:input>  
  114.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  115.    wsdl:input>  
  116.    <wsdl:output>  
  117.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  118.    wsdl:output>  
  119.    <wsdl:fault name="getHelloMessageFault">  
  120.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  121.    wsdl:fault>  
  122.   wsdl:operation>  
  123.   <wsdl:operation name="getMoreMessage">  
  124.    <soap12:operation soapAction="urn:getMoreMessage" style="document" />  
  125.    <wsdl:input>  
  126.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  127.    wsdl:input>  
  128.    <wsdl:output>  
  129.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  130.    wsdl:output>  
  131.    <wsdl:fault name="getMoreMessageFault">  
  132.     <soap12:body use="literal" namespace="http://service.util.danlley.org" />  
  133.    wsdl:fault>  
  134.   wsdl:operation>  
  135.  wsdl:binding>  
  136.  <wsdl:service name="HelloWorldAxiom">  
  137.   <wsdl:port name="HelloWorldAxiomSOAP11port" binding="axis2:HelloWorldAxiomSOAP11Binding">  
  138.    <soap:address location="http://localhost:8080/axis2/services/HelloWorldAxiom" />  
  139.   wsdl:port>  
  140.   <wsdl:port name="HelloWorldAxiomSOAP12port" binding="axis2:HelloWorldAxiomSOAP12Binding">  
  141.    <soap12:address location="http://localhost:8080/axis2/services/HelloWorldAxiom" />  
  142.   wsdl:port>  
  143.  wsdl:service>  
  144. wsdl:definitions>  


 
 
 
2.生成工程框架

运行命令如下:WSDL2Java -uri resources/META-INF/HelloWorldAxiom.wsdl -p org.danlley.service.adb -d adb -s -ss -sd -ssi -o build/service
如果得到以下结果则说明执行成功:

--------------------------------------------------------------------------------

D:\eclipse\workspace\axis2adblab>WSDL2Java -uri resources/META-INF/HelloWorldAxi
om.wsdl -p org.danlley.service.adb -d adb -s -ss -sd -ssi -o build/service
Using AXIS2_HOME:   D:\axis2-1.1.1
Using JAVA_HOME:    C:\Program Files\Java\jdk1.5.0_06
D:\eclipse\workspace\axis2adblab>

--------------------------------------------------------------------------------

 
 

执行成功后在工程中出现的路径如下:

--------------------------------------------------------------------------------

./build/service/build.xml

./build/service/resources/HelloWorldAxiom.wsdl
                                             services.xml

./build/service/src/org/danlley/service/adb/GetHelloMessageFaultException.java
                                                                           GetMoreMessageFaultException.java
                                                                           HelloWorldAxiomMessageReceiverInOut.java
                                                                           HelloWorldAxiomSkeleton.java
                                                                           HelloWorldAxiomSkeletonInterface.java

./build/service/src/org/danlley/util/service/xsd/ExtensionMapper.java
                                                                                 GetHelloMessage.java
                                                                                 GetHelloMessageFault.java
                                                                                 GetHelloMessageResponse.java
                                                                                 GetMoreMessage.java
                                                                                 GetMoreMessageFault.java
                                                                                 GetMoreMessageResponse.java

--------------------------------------------------------------------------------

 
 
 
 
 
3.分析框架结构
 
Axis·2的服务确实够周到的,不仅仅给我们把整个服务的框架都搭建好了,而且,还给我们顺便连编译工程所需用的Ant脚本都生成好了。真是不得不让人感叹一句"Apache办事,我放心!"呵呵呵。

让我们先大体看看生成的这些Java代码的作用,GetHelloMessageFaultException和GetMoreMessageFaultException是用来做异常处理的。关于这个话题我以纪在以前的专题中已经有过提及(也就是跟Fault相关的话题)。

说到Fault就不得不再提提SOAP 处理模型,Axis2 体系结构定义了两个管道(或流),分别称为 InPipe (InFlow) 和 OutPipe (OutFlow),用于处理服务器端的请求消息和响应消息。在客户端,这两个管道是反向的——换句话说,SOAP 请求消息流经 OutPipe,而响应消息流经 InPipe。管道或流包含一系列分为阶段的处理程序。除预先定义的阶段和处理程序集外,用户还可以在操作级别、服务级别或全局级别配置用户阶段和相关处理程序。处理程序充当 SOAP 消息的拦截器,可以处理 SOAP 消息的 Header 或 Body。InPipe 是通过以下阶段进行配置的:

--------------------------------------------------------------------------------

TransportIn
PreDispatch
Dispatch
PostDispatch
PolicyDetermination
User phases
Message validation

--------------------------------------------------------------------------------

 
 
 
请求消息在通过 Inpipe 中配置的所有阶段后,到达 MessageReceiver,然后由 MessageReceiver 调用实际服务实现。服务器的 OutPipe 包含以下阶段:

--------------------------------------------------------------------------------

Message initialization
Policy determination
User phases
MessageOut

--------------------------------------------------------------------------------


用户配置的阶段位于这两个管道的用户阶段(User phases) 部分。如果在执行这些管道的过程中发生错误,则这些错误将通过 InFaultPipe 或 OutFaultPipe 管道。收到 Fault 消息后,在客户端调用 InFaultPipe;如果某个调用导致将错误发送到客户端,则在服务器端调用 OutFaultPipe。用户可以将处理程序添加到预先定义的阶段,并且按照这些处理程序运行的顺序进行配置。

那么既然ADB pattern为我们提供了GetHelloMessageFaultException、GetMoreMessageFaultException、GetHelloMessageFault和GetMoreMessageFault类。那也就意味着我们可以对这些类进行扩充,以满足我们在实际当中的应用需求。由于我们Axiom的专题中举例时,两个实例均采用了in-Out模式,因此通过此工程的WSDL生成的ADB框架也就只有HelloWorldAxiomMessageReceiverInOut类来处理In-Out模式。GetHelloMessage和GetMoreMessage用来对接口HelloWorldAxiomSkeletonInterface的收取的数据进行封装,而GetHelloMessageResponse和GetMoreMessageResponse类则用于封装由HelloWorldAxiomSkeleton类处理后的结果。也就是说,真正的服务提供者就是HelloWorldAxiomSkeleton类。现在思路已经整理清楚,ADB生成的框架也基本上相当清楚了。那么剩下就是为我们新生成的框架动点小外科手术的时候了。
 
 
 
 
 
 
4.构建工程

根据我个人喜好,我的工程目录结构整理后如下:

--------------------------------------------------------------------------------

./build.xml

./resources/HelloWorldAxiom.wsdl
                     services.xml

./src/main/java/org/danlley/service/adb/GetHelloMessageFaultException.java
                                                                       GetMoreMessageFaultException.java
                                                                       HelloWorldAxiomMessageReceiverInOut.java
                                                                       HelloWorldAxiomSkeleton.java
                                                                       HelloWorldAxiomSkeletonInterface.java

./src/main/java/org/danlley/util/service/xsd/ExtensionMapper.java
                                                                             GetHelloMessage.java
                                                                             GetHelloMessageFault.java
                                                                             GetHelloMessageResponse.java
                                                                             GetMoreMessage.java
                                                                             GetMoreMessageFault.java
                                                                             GetMoreMessageResponse.java

--------------------------------------------------------------------------------

 

工程目录改变也就意味着我们需要修改Ant脚本,修改后的脚步如下:

分享到:
评论
4 楼 duzhijun1983 2008-06-12  
兄弟,你的代码顺序贴错了。
3 楼 danlley 2007-07-24  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:axis2="http://service.util.danlley.org"
 xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:ns="http://service.util.danlley.org/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" attributeFormDefault="qualified" elementFormDefault="qualified"
 targetNamespace="http://service.util.danlley.org/xsd">
 <xs:element name="getHelloMessageFault">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="getHelloMessageFault" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="getHelloMessage">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="element" nillable="true" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="getHelloMessageResponse">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="return" nillable="true" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="getMoreMessageFault">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="getMoreMessageFault" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="getMoreMessage">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="element" nillable="true" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="getMoreMessageResponse">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="return" nillable="true" type="xs:anyType" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>


这个是编写客户端必不可少的一个信息了。





6.生成客户端框架。

执行以下命令:

--------------------------------------------------------------------------------

WSDL2Java -uri resources/META-INF/HelloWorldAxiom.wsdl -p org.danlley.testcase.client -d adb -s -o build/client

--------------------------------------------------------------------------------


得到以下结果,那么说明客户端运行框架已经生成

--------------------------------------------------------------------------------

D:\eclipse\workspace\axis2adblab>WSDL2Java -uri resources/META-INF/HelloWorldAxi
om.wsdl -p org.danlley.testcase.client -d adb -s -o build/client
Using AXIS2_HOME:   D:\axis2-1.1.1
Using JAVA_HOME:    C:\Program Files\Java\jdk1.5.0_06
D:\eclipse\workspace\axis2adblab>

--------------------------------------------------------------------------------

根据我们的定义,该客户端框架在build/client目录下。将生成的客户端支持框架整理到我们的客户端程序存放路径下:src/test/java
目录结构如下:

--------------------------------------------------------------------------------
./src/test/java/org/danlley/testcase/client/GetHelloMessageFaultException.java
                                                                        GetMoreMessageFaultException.java
                                                                        HelloWorldAxiomStub.java

--------------------------------------------------------------------------------


在此目录下定义一个 ADBClient.java,也就是我们Web Services客户端。
实现ADBClient类(这里我们只实现了对getHelloMessage的处理):
package org.danlley.testcase.client;
import java.io.StringWriter;
import java.rmi.RemoteException;
import javax.xml.stream.XMLOutputFactory;
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.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.danlley.util.service.xsd.GetHelloMessage;
import org.danlley.util.service.xsd.GetHelloMessageResponse;
public class ADBClient{
    private static EndpointReference targetEPR=new EndpointReference("http://localhost:8080/axis2/services/HelloWorldAxiom");
    public static void main(String[] args){
        try{
            getHelloMessage("Hi, Danlley !");
            System.out.println("END<-----------------");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void getHelloMessage(String msg){
        try{
            HelloWorldAxiomStub stub=new HelloWorldAxiomStub("http://localhost:8080/axis2/services/HelloWorldAxiom");
            HelloWorldAxiomStub.GetHelloMessage helloMsg=new HelloWorldAxiomStub.GetHelloMessage();
            OMElement element=getOMElement(msg);
            helloMsg.setElement(element);
            System.out.println(helloMsg.getElement().toString());
            HelloWorldAxiomStub.GetHelloMessageResponse helloResponse;
            helloResponse=stub.getHelloMessage(helloMsg);
            OMElement return_val=helloResponse.get_return();
            System.out.println(return_val);
        }catch(AxisFault e){
            e.printStackTrace();
        }catch(RemoteException e){
            e.printStackTrace();
        }catch(GetHelloMessageFaultException e){
            e.printStackTrace();
        }
    }
    public static OMElement getOMElement(String msg){
        OMFactory fac=OMAbstractFactory.getOMFactory();
        OMNamespace omNs=fac.createOMNamespace("http://service.util.danlley.org/xsd","ns1");
        OMElement method=fac.createOMElement("getHelloMessage",omNs);
        OMElement value=fac.createOMElement("msg",omNs);
        value.addChild(fac.createOMText(value,msg));
        method.addChild(value);
        return method;
    }
}



运行客户端程序:

--------------------------------------------------------------------------------
<ns1:getHelloMessage xmlns:ns1="http://service.util.danlley.org/xsd"><ns1:msg>Hi, Danlley !</ns1:msg></ns1:getHelloMessage>
<ns:getHelloMessageResponse xmlns:ns="http://service.util.danlley.org/xsd"><ns:return>You are Perfect, Danlley!</ns:return></ns:getHelloMessageResponse>
END<-----------------

--------------------------------------------------------------------------------

那么一个基于ADB pattern的Web Services就实现了。








注:此文档在8月之前将一直处于维护状态


参考资料
http://ws.apache.org/axis2/1_2/index.html
http://blog.mllm.org/node/171
http://www-128.ibm.com/developerworks/cn/webservices/ws-webaxis1/#figure1
2 楼 danlley 2007-07-24  
打开HelloWorldAxiomSkeleton类:
package org.danlley.service.adb;
import org.danlley.util.service.xsd.GetHelloMessage;
import org.danlley.util.service.xsd.GetHelloMessageResponse;
import org.danlley.util.service.xsd.GetMoreMessage;
import org.danlley.util.service.xsd.GetMoreMessageResponse;
public class HelloWorldAxiomSkeleton implements HelloWorldAxiomSkeletonInterface{
    public GetMoreMessageResponse getMoreMessage(GetMoreMessage param4) throws GetMoreMessageFaultException{
        //Todo fill this with the necessary business logic
        throw new UnsupportedOperationException("Please implement "+this.getClass().getName()+"#getMoreMessage");
    }
    public GetHelloMessageResponse getHelloMessage(GetHelloMessage param6) throws GetHelloMessageFaultException{
        //Todo fill this with the necessary business logic
        throw new UnsupportedOperationException("Please implement "+this.getClass().getName()+"#getHelloMessage");
    }
}




实现HelloWorldAxiomSkeleton类:
package org.danlley.service.adb;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.danlley.util.service.xsd.GetHelloMessage;
import org.danlley.util.service.xsd.GetHelloMessageResponse;
import org.danlley.util.service.xsd.GetMoreMessage;
import org.danlley.util.service.xsd.GetMoreMessageResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
public class HelloWorldAxiomSkeleton implements HelloWorldAxiomSkeletonInterface{
    public GetMoreMessageResponse getMoreMessage(GetMoreMessage param4) throws GetMoreMessageFaultException{
        try{
            OMElement element=param4.getElement();
            element.build();
            element.detach();
            DocumentFactory dom=new DocumentFactory();
            dom.createDocument(element.toString());
            System.out.println(element.toString());
            Document doc=DocumentHelper.parseText(element.toString());
            String msg=doc.selectSingleNode("//tns:msg").getText();
            String userPWD=doc.selectSingleNode("//tns:userPWD").getText();
            String transactionKey=doc.selectSingleNode("//tns:transactionKey").getText();
            System.out.println("\n msg="+msg+"\n userPWD="+userPWD+"\n transactionKey="+transactionKey);
            OMFactory fac=OMAbstractFactory.getOMFactory();
            OMNamespace omNs=fac.createOMNamespace("http://service.util.danlley.org/xsd","ns");
            OMElement method=fac.createOMElement("getHelloMessageResponse",omNs);
            OMElement value=fac.createOMElement("return",omNs);
            String returnText="MultiMessage for U, Danlley!";
            returnText=returnText+"\n msg="+msg+"\t userPWD="+userPWD+"\t transactionKey="+transactionKey;
            value.addChild(fac.createOMText(value,returnText));
            method.addChild(value);
            GetMoreMessageResponse response=new GetMoreMessageResponse();
            response.set_return(method);
            return response;
        }catch(OMException e){
            e.printStackTrace();
            return null;
        }catch(DocumentException e){
            e.printStackTrace();
            return null;
        }
    }
    public GetHelloMessageResponse getHelloMessage(GetHelloMessage param6) throws GetHelloMessageFaultException{
        OMElement element=param6.getElement();
        element.build();
        element.detach();
        OMElement symbolElement=element.getFirstElement();
        String helloworld=symbolElement.getText();
        System.out.println("We got the messages :"+(helloworld!=null?helloworld:"No Msg!"));
        String returnText="You are Perfect, Danlley!";
        OMFactory fac=OMAbstractFactory.getOMFactory();
        OMNamespace omNs=fac.createOMNamespace("http://service.util.danlley.org/xsd","ns");
        OMElement method=fac.createOMElement("getHelloMessageResponse",omNs);
        OMElement value=fac.createOMElement("return",omNs);
        value.addChild(fac.createOMText(value,returnText));
        method.addChild(value);
        GetHelloMessageResponse response=new GetHelloMessageResponse();
        response.set_return(method);
        return response;
    }
}



5.发布、部署

执行ant脚本声称aar包。部署aar到tomcat目录下,在浏览器中访问:http://localhost:8080/axis2/services/HelloWorldAxiom?wsdl
就可以看到我们生成的新的WSDL。由于WSDL 文件过于繁冗,让我们看看XSD文件就已经足够了,访问地址:http://localhost:8080/axis2/services/HelloWorldAxiom?xsd 可以看到如下内容:
1 楼 danlley 2007-07-24  
<project basedir="." default="jar.server">
 <!--Auto generated ant build file-->
 <property environment="env" />
 <property name="axis2.home" value="${env.AXIS2_HOME}" />
 <property name="project.base.dir" value="." />
 <property name="maven.class.path" value="" />
 <property name="name" value="HelloWorldAxiom" />
 <property name="src" value="${project.base.dir}/src/main/java" />
 <property name="test" value="${project.base.dir}/src/test/java" />
 <property name="build" value="${project.base.dir}/build" />
 <property name="classes" value="${build}/classes" />
 <property name="lib" value="${build}/lib" />
 <property name="resources" value="${project.base.dir}/resources" />
 <property value="" name="jars.ok" />
 <path id="axis2.class.path">
  <pathelement path="${java.class.path}" />
  <pathelement path="${maven.class.path}" />
  <fileset dir="${axis2.home}">
   <include name="lib/*.jar" />
  </fileset>
 </path>
 <target name="init">
  <mkdir dir="${build}" />
  <mkdir dir="${classes}" />
  <mkdir dir="${lib}" />
  <mkdir dir="${test}" />
 </target>
 <target depends="init" name="pre.compile.test">
  <!--Test the classpath for the availability of necesary classes-->
  <available classpathref="axis2.class.path" property="stax.available" classname="javax.xml.stream.XMLStreamReader" />
  <available classpathref="axis2.class.path" property="axis2.available" classname="org.apache.axis2.engine.AxisEngine" />
  <condition property="jars.ok">
   <and>
    <isset property="stax.available" />
    <isset property="axis2.available" />
   </and>
  </condition>
  <!--Print out the availabilities-->
  <echo message="Stax Availability= ${stax.available}" />
  <echo message="Axis2 Availability= ${axis2.available}" />
 </target>
 <target depends="pre.compile.test" name="compile.src" if="jars.ok">
  <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
   <classpath refid="axis2.class.path" />
  </javac>
 </target>
 <target depends="compile.src" name="compile.test" if="jars.ok">
  <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}">
   <src path="${test}" />
   <classpath refid="axis2.class.path" />
  </javac>
 </target>
 <target depends="pre.compile.test" name="echo.classpath.problem" unless="jars.ok">
  <echo message="The class path is not set right!                                Please make sure the following classes are in the classpath                                1. XmlBeans                                2. Stax                                3. Axis2                 " />
 </target>
 <target depends="jar.server, jar.client" name="jar.all" />
 <target depends="compile.src,echo.classpath.problem" name="jar.server" if="jars.ok">
  <copy toDir="${classes}/META-INF" failonerror="false">
   <fileset dir="${resources}">
    <include name="*.xml" />
    <include name="*.wsdl" />
    <include name="*.xsd" />
   </fileset>
  </copy>
  <jar destfile="${lib}/${name}.aar">
   <fileset excludes="**/Test.class" dir="${classes}" />
  </jar>
 </target>
 <target if="jars.ok" name="jar.client" depends="compile.test">
  <jar destfile="${lib}/${name}-test-client.jar">
   <fileset dir="${classes}">
    <exclude name="**/META-INF/*.*" />
    <exclude name="**/lib/*.*" />
    <exclude name="**/*MessageReceiver.class" />
    <exclude name="**/*Skeleton.class" />
   </fileset>
  </jar>
 </target>
 <target if="jars.ok" name="make.repo" depends="jar.server">
  <mkdir dir="${build}/repo/" />
  <mkdir dir="${build}/repo/services" />
  <copy file="${build}/lib/${name}.aar" toDir="${build}/repo/services/" />
 </target>
 <target if="jars.ok" name="start.server" depends="make.repo">
  <java fork="true" classname="org.apache.axis2.transport.http.SimpleHTTPServer">
   <arg value="${build}/repo" />
   <classpath refid="axis2.class.path" />
  </java>
 </target>
 <target depends="compile.test" name="run.test" if="jars.ok">
  <path id="test.class.path">
   <pathelement location="${lib}/${name}-test-client.jar" />
   <path refid="axis2.class.path" />
  </path>
  <mkdir dir="${build}/test-reports/" />
  <junit haltonfailure="yes" printsummary="yes">
   <classpath refid="test.class.path" />
   <formatter type="plain" />
   <batchtest fork="yes" toDir="${build}/test-reports/">
    <fileset dir="${test}">
     <include name="**/*Test*.java" />
    </fileset>
   </batchtest>
  </junit>
 </target>
 <target name="clean">
  <delete dir="${build}" />
 </target>
</project>

相关推荐

    使用axis实现web]Services服务

    Java 中,使用axis来实现webServices 里面包含Word详细说明使用webservices的步骤, 看了就会啦, 使用webServicers里面jar ,和工具都包含在里面。。。 如果你要实现webservies的话, 不看后悔死你。。。。

    axis2开发Web Services入门

    ### Axis2 开发 Web Services 入门 #### 知识点概述 本文旨在介绍如何使用 Axis2 开发 Web Services 的全过程,包括环境搭建、插件安装等基础准备工作,以及具体的开发流程与实例演示。 #### 1. 环境搭建 ##### ...

    Apache Axis2 Web Services, 2nd Edition.pdf

    Chapter 1, Apache Web Services and Axis2 - Gives you an introduction to web services and the web service stack at Apache. Chapter 2, Looking inside Axis2 - Learn about Axis2 architecture and the ...

    Java+Axis2调用Web Services 网络接口

    Java和Axis2是开发Web服务客户端的重要工具,用于调用基于SOAP协议的Web服务。本文将深入探讨如何利用Java和Axis2库来实现这一功能,同时结合提供的代码示例进行详细解析。 首先,Web服务是一种通过网络进行通信的...

    axis2发布webServices的两种方式

    2. **生成服务描述文件(WSDL)**:基于服务类,使用Axis2工具生成WSDL(Web Services Description Language)文件,描述服务的接口、操作和绑定。 3. **创建服务 Archive (AAR) 文件**:将服务类、依赖库和WSDL打包...

    Apache Axis2 Web Services的电子书

    包括3本Axis2的书(英文),實為2本(第1本有...1.Developing Web Services with Apache CXF and Axis2, 3rd Edition 2.Packt.Publishing.Quickstart.Apache.Axis2 3.Develop Web Services With Apache Axis2 PDF格式

    Apache Axis2 Web Services, 2nd

    Extensive and detailed coverage of the enterprise ready Apache Axis2 Web Services / SOAP / WSDL engine. Attain a more flexible and extensible framework with the world class Axis2 architecture. Learn ...

    axis2开发web services 所需jar包

    9. **axis2-jaxws.jar**:支持JAX-WS(Java API for XML Web Services)规范,使得Axis2能够与Java SE和EE环境无缝集成。 10. **log4j.jar, slf4j-api.jar, slf4j-log4j12.jar**:日志框架,用于记录开发和运行时的...

    在Eclipse中创建基于Axis2的web services

    Axis2是Apache的一个项目,它提供了一种高效的Web服务实现框架,支持SOAP和RESTful风格的服务。以下是如何在Eclipse环境中利用Axis2来创建Web服务的详细步骤: 首先,确保你已经安装了Eclipse IDE,并且安装了相关...

    axis 实现 web services java

    axis 实现 web services java 实例 webservices 参考:http://blog.csdn.net/chinarenzhou/article/details/7609953

    axis1开发Web Services入门

    ### 使用Axis1开发Web Services入门知识点详解 #### 一、实验背景与目标 在学习如何使用Axis1开发Web Services之前,我们首先需要了解几个基本概念:Web Services是一种平台独立的服务形式,它允许不同应用程序...

    Web Services Axis实现

    【Web Services Axis实现】 Web Services是一种基于互联网的、平台无关的分布式计算模型,它允许不同系统间的应用程序通过网络进行通信和交互。Axis是Apache软件基金会开发的一个开源工具,专门用于实现Web ...

    Axis2创建webservice服务的方式 之 ADB 方式(二)

    本篇将详细介绍使用Axis2通过ADB(Abstract Data Binding)方式创建Web服务的方法。 ADB,全称Abstract Data Binding,是一种轻量级的数据绑定框架,它简化了SOAP消息到Java对象的映射过程。与传统的JavaBeans或...

    Axis2集成Spring发布WebServices服务

    通过上述步骤,我们可以成功地将Axis2与Spring框架集成在一起,实现WebServices服务的发布。这种方式不仅充分利用了Axis2在Web服务领域的优势,同时也发挥了Spring框架在管理Bean和服务实现方面的强大功能。对于企业...

    axis2 webservices 例子

    本示例将详细介绍如何使用JAX-WS(Java API for XML Web Services)发布Web服务,并利用Axis2作为客户端获取Web服务的数据。 首先,让我们深入理解JAX-WS。JAX-WS是Java平台的标准组件,它简化了创建和消费Web服务...

    axis2 1.7.4war及已经集成了axis2的web工程

    也可以自行去 apache官网下载 ,另外还包含一个已经集成了axis2的web工程源码 使用的jdk1.6开发,可以直接把web工程导入到myeclipse,部署到tomcat可运行后,浏览器输入 http://localhost:端口号/Axis2Web/services/...

    Axis2之使用services.xml发布带包的Webservice

    标题中的“Axis2之使用services.xml发布带包的Webservice”指的是在Apache Axis2框架下,通过services.xml配置文件来发布包含多个类或包的Web服务。Apache Axis2是Java平台上的一个开源Web服务框架,它允许开发人员...

Global site tag (gtag.js) - Google Analytics