`
icarusliu
  • 浏览: 232160 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

详解Axis2实现Web Services之POJOs篇

阅读更多
在Axis2对Web Services的众多实现方式中,POJOs方式很明显是最简单的一种了。对于Axis2的相关配置,由于我在此前专题(http://danlley.iteye.com/blog/101975)中已经进行过相关的说明,因此,这里我就不再重复阐述了。
 
 
 
为了能够很快进入状态,我们立即开始一个简单的工程,用来说明POJOs的发布和使用,该例子程序用于完成一个简单的更新和查询操作。
 
定义服务类
xml 代码
  1. package samples.quickstart.service.pojo;   
  2. import java.util.HashMap;   
  3. public class StockQuoteService {   
  4.     private HashMap map = new HashMap();   
  5.     public double getPrice(String symbol) {   
  6.         Double price = (Double) map.get(symbol);   
  7.         if(price != null){   
  8.             return price.doubleValue();   
  9.         }   
  10.         return 42.00;   
  11.     }   
  12.     public void update(String symbol, double price) {   
  13.         map.put(symbol, new Double(price));   
  14.     }   
  15. }  
 
给工程编写Ant脚本:
xml 代码
  1. <project basedir="." default="generate.service">  
  2.  <property name="build.dir" value="build" />  
  3.  <target name="compile.service">  
  4.   <mkdir dir="${build.dir}" />  
  5.   <mkdir dir="${build.dir}/classes" />  
  6.   <javac debug="on" fork="true" destdir="${build.dir}/classes" srcdir="${basedir}/src/main/java">  
  7.   javac>  
  8.  target>  
  9.  <target name="generate.service" depends="compile.service">  
  10.   <copy toDir="${build.dir}/classes" failonerror="false">  
  11.    <fileset dir="${basedir}/src/main/resources">  
  12.     <include name="**/*.xml" />  
  13.    fileset>  
  14.   copy>  
  15.   <jar destfile="${build.dir}/StockQuoteService.aar">  
  16.    <fileset excludes="**/Test.class" dir="${build.dir}/classes" />  
  17.   jar>  
  18.  target>  
  19. project>  
 
从Ant脚本的配置可以看出我们的工程相关配置:

工程源代码路径:src/main/java
资源文件路径:src/main/resources
编译路径:build

 
 
 
在src/main/resources路径下新建一个META-INF路径,在该路径下定义services.xml,内容如下:
xml 代码
  1. <service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/">  
  2.     <description>Stock Quote Servicedescription>  
  3.     <messageReceivers>  
  4.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"  
  5.                          class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>  
  6.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  7.                          class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>  
  8.     messageReceivers>  
  9.     <schema schemaNamespace="http://quickstart.samples/xsd"/>  
  10.     <parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteServiceparameter>  
  11. service>  
这样所有的事情都已经准备好了,好像没有引用AXIS2的资源文件呀!?需要吗!呵呵呵呵,当然不需要!谁让我是POJOs呢。
 
 
 
 
从上面的配置可以看出,工程中除了多出一个services.xml文件外,工程几乎与普通工程没什么两样。这也是POJOs中很值得称道的一点——简单!services.xml中对MEP(Message Exchange Pattern)的声明中也是格外的粗粒度。它笼统的告诉Web Services容器,我这里的方法需要支持in模式和in-out模式两种方式,至于是哪个方法支持哪一种模式,我不告诉你,自己去猜吧。呵呵呵呵。我只告诉你,我的ServiceClass是samples.quickstart.service.pojo.StockQuoteService
 我需要在这里做一点申明:services.xml的粗粒度声明,纯粹属于我个人的“偷懒”行为,而实际上在实际的应用当中,为了明确期间,一般是不会采取我这种处理方式的。通常都需要对每个方法说明MEP方式
 
 
很明显Axis2最后需要的发布文件是一个aar包。但是我要说明的一点就是,我们这里的aar包的包名必须与ServiceClass的名称(在我们这个工程当中,也就是StockQuoteService)完全一致(包括大小写)。
 
 
 
 
在工程根目录下打开命令行运行ant脚本(前提是你已经配置了Ant环境变量),执行结果如下:

D:\eclipse\workspace\axis2pojoslab>ant
Buildfile: build.xml
compile.service:
    [mkdir] Created dir: D:\eclipse\workspace\axis2pojoslab\build
    [mkdir] Created dir: D:\eclipse\workspace\axis2pojoslab\build\classes
    [javac] Compiling 1 source file to D:\eclipse\workspace\axis2pojoslab\build\
classes
    [javac] 注意: D:\eclipse\workspace\axis2pojoslab\src\main\java\samples\quic
kstart\service\pojo\StockQuoteService.java 使用了未经检查或不安全的操作。
    [javac] 注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。
generate.service:
     [copy] Copying 1 file to D:\eclipse\workspace\axis2pojoslab\build\classes
      [jar] Building jar: D:\eclipse\workspace\axis2pojoslab\build\StockQuoteSer
vice.aar
BUILD SUCCESSFUL
Total time: 2 seconds

 
 
这时,在工程的build路径下就会出现已经打包成功的aar包:StockQuoteService.aar。打开此aar包,看看META-INF路径,是否在该路径存在services.xml。确定存在后将其copy到服务器中,本人用的是tomcat6:D:\tomcat6\webapps\axis2\WEB-INF\services启动服务!在IE中访问以下地址:http://localhost/axis2/services/StockQuoteService?wsdl
 
 
我们得到以下内容:
xml 代码
  1. <wsdl:definitions xmlns:axis2="http://quickstart.samples/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:ns0="http://quickstart.samples/xsd" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://quickstart.samples/">  
  2.   <wsdl:documentation>Stock Quote Servicewsdl:documentation>    
  3. <wsdl:types>  
  4. <xs:schema xmlns:ns="http://quickstart.samples/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">  
  5. <xs:element name="getPrice">  
  6. <xs:complexType>  
  7. <xs:sequence>  
  8.   <xs:element name="symbol" nillable="true" type="xs:string" />    
  9.   xs:sequence>  
  10.   xs:complexType>  
  11.   xs:element>  
  12. <xs:element name="getPriceResponse">  
  13. <xs:complexType>  
  14. <xs:sequence>  
  15.   <xs:element name="return" nillable="true" type="xs:double" />    
  16.   xs:sequence>  
  17.   xs:complexType>  
  18.   xs:element>  
  19. <xs:element name="update">  
  20. <xs:complexType>  
  21. <xs:sequence>  
  22.   <xs:element name="symbol" nillable="true" type="xs:string" />    
  23.   <xs:element name="price" nillable="true" type="xs:double" />    
  24.   xs:sequence>  
  25.   xs:complexType>  
  26.   xs:element>  
  27.   xs:schema>  
  28.   wsdl:types>  
  29. <wsdl:message name="updateMessage">  
  30.   <wsdl:part name="part1" element="ns0:update" />    
  31.   wsdl:message>  
  32. <wsdl:message name="getPriceMessage">  
  33.   <wsdl:part name="part1" element="ns0:getPrice" />    
  34.   wsdl:message>  
  35. <wsdl:message name="getPriceResponse">  
  36.   <wsdl:part name="part1" element="ns0:getPriceResponse" />    
  37.   wsdl:message>  
  38. <wsdl:portType name="StockQuoteServicePortType">  
  39. <wsdl:operation name="update">  
  40.   <wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:updateMessage" wsaw:Action="urn:update" />    
  41.   wsdl:operation>  
  42. <wsdl:operation name="getPrice">  
  43.   <wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:getPriceMessage" wsaw:Action="urn:getPrice" />    
  44.   <wsdl:output message="axis2:getPriceResponse" />    
  45.   wsdl:operation>  
  46.   wsdl:portType>  
  47. <wsdl:binding name="StockQuoteServiceSOAP11Binding" type="axis2:StockQuoteServicePortType">  
  48.   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />    
  49. <wsdl:operation name="update">  
  50.   <soap:operation soapAction="urn:update" style="document" />    
  51. <wsdl:input>  
  52.   <soap:body use="literal" />    
  53.   wsdl:input>  
  54.   wsdl:operation>  
  55. <wsdl:operation name="getPrice">  
  56.   <soap:operation soapAction="urn:getPrice" style="document" />    
  57. <wsdl:input>  
  58.   <soap:body use="literal" />    
  59.   wsdl:input>  
  60. <wsdl:output>  
  61.   <soap:body use="literal" />    
  62.   wsdl:output>  
  63.   wsdl:operation>  
  64.   wsdl:binding>  
  65. <wsdl:binding name="StockQuoteServiceSOAP12Binding" type="axis2:StockQuoteServicePortType">  
  66.   <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />    
  67. <wsdl:operation name="update">  
  68.   <soap12:operation soapAction="urn:update" style="document" />    
  69. <wsdl:input>  
  70.   <soap12:body use="literal" />    
  71.   wsdl:input>  
  72.   wsdl:operation>  
  73. <wsdl:operation name="getPrice">  
  74.   <soap12:operation soapAction="urn:getPrice" style="document" />    
  75. <wsdl:input>  
  76.   <soap12:body use="literal" />    
  77.   wsdl:input>  
  78. <wsdl:output>  
  79.   <soap12:body use="literal" />    
  80.   wsdl:output>  
  81.   wsdl:operation>  
  82.   wsdl:binding>  
  83. <wsdl:binding name="StockQuoteServiceHttpBinding" type="axis2:StockQuoteServicePortType">  
  84.   <http:binding verb="POST" />    
  85. <wsdl:operation name="update">  
  86.   <http:operation location="update" />    
  87. <wsdl:input>  
  88.   <mime:content type="text/xml" />    
  89.   wsdl:input>  
  90.   wsdl:operation>  
  91. <wsdl:operation name="getPrice">  
  92.   <http:operation location="getPrice" />    
  93. <wsdl:input>  
  94.   <mime:content type="text/xml" />    
  95.   wsdl:input>  
  96. <wsdl:output>  
  97.   <mime:content type="text/xml" />    
  98.   wsdl:output>  
  99.   wsdl:operation>  
  100.   wsdl:binding>  
  101. <wsdl:service name="StockQuoteService">  
  102. <wsdl:port name="StockQuoteServiceSOAP11port_http" binding="axis2:StockQuoteServiceSOAP11Binding">  
  103.   <soap:address location="http://localhost:80/axis2/services/StockQuoteService" />    
  104.   wsdl:port>  
  105. <wsdl:port name="StockQuoteServiceSOAP12port_http" binding="axis2:StockQuoteServiceSOAP12Binding">  
  106.   <soap12:address location="http://localhost:80/axis2/services/StockQuoteService" />    
  107.   wsdl:port>  
  108. <wsdl:port name="StockQuoteServiceHttpport1" binding="axis2:StockQuoteServiceHttpBinding">  
  109.   <http:address location="http://localhost:80/axis2/rest/StockQuoteService" />    
  110.   wsdl:port>  
  111.   wsdl:service>  
  112.   wsdl:definitions>  
 
这就是大名鼎鼎的WSDL了,我们当初没有定义这个文件的原因就是容器在运行过程中会自动根据你的services.xml中的配置信息生成我们所必需的WSDL。
 
 
 
任何信息人们总会要求他详细点,然后可以了解更多,但是有时候详细也会带来很多不便,到目前为止,我看到WSDL的时候还是有点晕,呵呵呵呵,尤其是当其中的方法较多的情况下,简直能够让人崩溃。怎么办?访问这个地址吧:http://localhost/axis2/services/StockQuoteService?xsd
(tomcat在默认情况下的端口为8080,本人一向比较懒惰为了访问时少输入点东西在地址栏,我把端口改成了80)
xml 代码
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:axis2="http://quickstart.samples/" xmlns:ns="http://quickstart.samples/xsd" xmlns:ns0="http://quickstart.samples/xsd" xmlns:ns1="http://org.apache.axis2/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">  
  2. <xs:element name="getPrice">  
  3. <xs:complexType>  
  4. <xs:sequence>  
  5.   <xs:element name="symbol" nillable="true" type="xs:string" />    
  6.   xs:sequence>  
  7.   xs:complexType>  
  8.   xs:element>  
  9. <xs:element name="getPriceResponse">  
  10. <xs:complexType>  
  11. <xs:sequence>  
  12.   <xs:element name="return" nillable="true" type="xs:double" />    
  13.   xs:sequence>  
  14.   xs:complexType>  
  15.   xs:element>  
  16. <xs:element name="update">  
  17. <xs:complexType>  
  18. <xs:sequence>  
  19.   <xs:element name="symbol" nillable="true" type="xs:string" />    
  20.   <xs:element name="price" nillable="true" type="xs:double" />    
  21.   xs:sequence>  
  22.   xs:complexType>  
  23.   xs:element>  
  24.   xs:schema>  
这些信息已经足够满足我们编写客户端程序的需要了。其中的symbol是我们在调用getPrice过程中需要传入的参数,而return是返回的参数。由于update方法不需要返回值,因此只有传入的参数symbolprice。这些信息是我们在下面的操作中都需要用到的。
 
不过在这个例子中,我们甚至根本就不需要编写客户端程序。仅仅通过访问就可以执行服务提供的接口。
 
xml 代码
  1. <ns:getPriceResponse xmlns:ns="http://quickstart.samples/xsd">  
  2.   <ns:return>42.0ns:return>    
  3.   ns:getPriceResponse>  
这个是我们的程序在没有任何数据的情况下返回的默认值。
 
 
 
接下来让我们来更新数据:
 
你会发现执行完后没有任何反应,没关系让我们继续回过来在访问地址http://localhost/axis2/rest/StockQuoteService/getPrice?symbol=danlley
xml 代码
  1. <ns:getPriceResponse xmlns:ns="http://quickstart.samples/xsd">  
  2.   <ns:return>100.0ns:return>    
  3.   ns:getPriceResponse>  
是不是数据已经成功得到修改了?!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics