---
title: "详解Axis2实现Web Services之POJOs篇"
author: "danlley"
date: "2007-07-17T22:25:17Z"
tags: []
slug: "axis2-web-services-pojos"
draft: false
lastmod: "2007-07-17T22:25:17Z"
id: 102163
url: "https://danlley.iteye.com/blog/102163"
---

在Axis2对Web Services的众多实现方式中，POJOs方式很明显是最简单的一种了。对于Axis2的相关配置，由于我在此前专题（http://danlley.iteye.com/blog/101975）中已经进行过相关的说明，因此，这里我就不再重复阐述了。

 

 

 

为了能够很快进入状态，我们立即开始一个简单的工程，用来说明POJOs的发布和使用，该例子程序用于完成一个简单的更新和查询操作。

 

定义服务类

xml 代码

01. package samples.quickstart.service.pojo;
02. import java.util.HashMap;
03. public class StockQuoteService {
04. private HashMap map = new HashMap();
05. public double getPrice(String symbol) {
06. Double price = (Double) map.get(symbol);
07. if(price != null){
08. return price.doubleValue();
09. }
10. return 42.00;
11. }
12. public void update(String symbol, double price) {
13. map.put(symbol, new Double(price));
14. }
15. }

 

给工程编写Ant脚本：

xml 代码

01. &lt;project basedir="." default="generate.service"&gt;
02. &lt;property name="build.dir" value="build" /&gt;
03. &lt;target name="compile.service"&gt;
04. &lt;mkdir dir="${build.dir}" /&gt;
05. &lt;mkdir dir="${build.dir}/classes" /&gt;
06. &lt;javac debug="on" fork="true" destdir="${build.dir}/classes" srcdir="${basedir}/src/main/java"&gt;
07. javac&gt;
08. target&gt;
09. &lt;target name="generate.service" depends="compile.service"&gt;
10. &lt;copy toDir="${build.dir}/classes" failonerror="false"&gt;
11. &lt;fileset dir="${basedir}/src/main/resources"&gt;
12. &lt;include name="\*\*/\*.xml" /&gt;
13. fileset&gt;
14. copy&gt;
15. &lt;jar destfile="${build.dir}/StockQuoteService.aar"&gt;
16. &lt;fileset excludes="\*\*/Test.class" dir="${build.dir}/classes" /&gt;
17. jar&gt;
18. target&gt;
19. project&gt;

 

从Ant脚本的配置可以看出我们的工程相关配置：

* * *

工程源代码路径：src/main/java

资源文件路径：src/main/resources

编译路径：build

* * *

 

 

 

在src/main/resources路径下新建一个META-INF路径，在该路径下定义services.xml，内容如下：

xml 代码

01. &lt;service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/"&gt;
02. &lt;description&gt;Stock Quote Servicedescription&gt;
03. &lt;messageReceivers&gt;
04. &lt;messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
05. class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/&gt;
06. &lt;messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
07. class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/&gt;
08. messageReceivers&gt;
09. &lt;schema schemaNamespace="http://quickstart.samples/xsd"/&gt;
10. &lt;parameter name="ServiceClass"&gt;samples.quickstart.service.pojo.StockQuoteServiceparameter&gt;
11. service&gt;

这样所有的事情都已经准备好了，好像没有引用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&gt;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](http://localhost/axis2/services/StockQuoteService?wsdl)

 

 

我们得到以下内容：

xml 代码

001. &lt;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/"&gt;
002. &lt;wsdl:documentation&gt;Stock Quote Servicewsdl:documentation&gt;
003. - &lt;wsdl:types&gt;
004. - &lt;xs:schema xmlns:ns="http://quickstart.samples/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd"&gt;
005. - &lt;xs:element name="getPrice"&gt;
006. - &lt;xs:complexType&gt;
007. - &lt;xs:sequence&gt;
008. &lt;xs:element name="symbol" nillable="true" type="xs:string" /&gt;
009. xs:sequence&gt;
010. xs:complexType&gt;
011. xs:element&gt;
012. - &lt;xs:element name="getPriceResponse"&gt;
013. - &lt;xs:complexType&gt;
014. - &lt;xs:sequence&gt;
015. &lt;xs:element name="return" nillable="true" type="xs:double" /&gt;
016. xs:sequence&gt;
017. xs:complexType&gt;
018. xs:element&gt;
019. - &lt;xs:element name="update"&gt;
020. - &lt;xs:complexType&gt;
021. - &lt;xs:sequence&gt;
022. &lt;xs:element name="symbol" nillable="true" type="xs:string" /&gt;
023. &lt;xs:element name="price" nillable="true" type="xs:double" /&gt;
024. xs:sequence&gt;
025. xs:complexType&gt;
026. xs:element&gt;
027. xs:schema&gt;
028. wsdl:types&gt;
029. - &lt;wsdl:message name="updateMessage"&gt;
030. &lt;wsdl:part name="part1" element="ns0:update" /&gt;
031. wsdl:message&gt;
032. - &lt;wsdl:message name="getPriceMessage"&gt;
033. &lt;wsdl:part name="part1" element="ns0:getPrice" /&gt;
034. wsdl:message&gt;
035. - &lt;wsdl:message name="getPriceResponse"&gt;
036. &lt;wsdl:part name="part1" element="ns0:getPriceResponse" /&gt;
037. wsdl:message&gt;
038. - &lt;wsdl:portType name="StockQuoteServicePortType"&gt;
039. - &lt;wsdl:operation name="update"&gt;
040. &lt;wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:updateMessage" wsaw:Action="urn:update" /&gt;
041. wsdl:operation&gt;
042. - &lt;wsdl:operation name="getPrice"&gt;
043. &lt;wsdl:input xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" message="axis2:getPriceMessage" wsaw:Action="urn:getPrice" /&gt;
044. &lt;wsdl:output message="axis2:getPriceResponse" /&gt;
045. wsdl:operation&gt;
046. wsdl:portType&gt;
047. - &lt;wsdl:binding name="StockQuoteServiceSOAP11Binding" type="axis2:StockQuoteServicePortType"&gt;
048. &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /&gt;
049. - &lt;wsdl:operation name="update"&gt;
050. &lt;soap:operation soapAction="urn:update" style="document" /&gt;
051. - &lt;wsdl:input&gt;
052. &lt;soap:body use="literal" /&gt;
053. wsdl:input&gt;
054. wsdl:operation&gt;
055. - &lt;wsdl:operation name="getPrice"&gt;
056. &lt;soap:operation soapAction="urn:getPrice" style="document" /&gt;
057. - &lt;wsdl:input&gt;
058. &lt;soap:body use="literal" /&gt;
059. wsdl:input&gt;
060. - &lt;wsdl:output&gt;
061. &lt;soap:body use="literal" /&gt;
062. wsdl:output&gt;
063. wsdl:operation&gt;
064. wsdl:binding&gt;
065. - &lt;wsdl:binding name="StockQuoteServiceSOAP12Binding" type="axis2:StockQuoteServicePortType"&gt;
066. &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /&gt;
067. - &lt;wsdl:operation name="update"&gt;
068. &lt;soap12:operation soapAction="urn:update" style="document" /&gt;
069. - &lt;wsdl:input&gt;
070. &lt;soap12:body use="literal" /&gt;
071. wsdl:input&gt;
072. wsdl:operation&gt;
073. - &lt;wsdl:operation name="getPrice"&gt;
074. &lt;soap12:operation soapAction="urn:getPrice" style="document" /&gt;
075. - &lt;wsdl:input&gt;
076. &lt;soap12:body use="literal" /&gt;
077. wsdl:input&gt;
078. - &lt;wsdl:output&gt;
079. &lt;soap12:body use="literal" /&gt;
080. wsdl:output&gt;
081. wsdl:operation&gt;
082. wsdl:binding&gt;
083. - &lt;wsdl:binding name="StockQuoteServiceHttpBinding" type="axis2:StockQuoteServicePortType"&gt;
084. &lt;http:binding verb="POST" /&gt;
085. - &lt;wsdl:operation name="update"&gt;
086. &lt;http:operation location="update" /&gt;
087. - &lt;wsdl:input&gt;
088. &lt;mime:content type="text/xml" /&gt;
089. wsdl:input&gt;
090. wsdl:operation&gt;
091. - &lt;wsdl:operation name="getPrice"&gt;
092. &lt;http:operation location="getPrice" /&gt;
093. - &lt;wsdl:input&gt;
094. &lt;mime:content type="text/xml" /&gt;
095. wsdl:input&gt;
096. - &lt;wsdl:output&gt;
097. &lt;mime:content type="text/xml" /&gt;
098. wsdl:output&gt;
099. wsdl:operation&gt;
100. wsdl:binding&gt;
101. - &lt;wsdl:service name="StockQuoteService"&gt;
102. - &lt;wsdl:port name="StockQuoteServiceSOAP11port\_http" binding="axis2:StockQuoteServiceSOAP11Binding"&gt;
103. &lt;soap:address location="http://localhost:80/axis2/services/StockQuoteService" /&gt;
104. wsdl:port&gt;
105. - &lt;wsdl:port name="StockQuoteServiceSOAP12port\_http" binding="axis2:StockQuoteServiceSOAP12Binding"&gt;
106. &lt;soap12:address location="http://localhost:80/axis2/services/StockQuoteService" /&gt;
107. wsdl:port&gt;
108. - &lt;wsdl:port name="StockQuoteServiceHttpport1" binding="axis2:StockQuoteServiceHttpBinding"&gt;
109. &lt;http:address location="http://localhost:80/axis2/rest/StockQuoteService" /&gt;
110. wsdl:port&gt;
111. wsdl:service&gt;
112. wsdl:definitions&gt;

 

这就是大名鼎鼎的WSDL了，我们当初没有定义这个文件的原因就是容器在运行过程中会自动根据你的services.xml中的配置信息生成我们所必需的WSDL。

 

 

 

任何信息人们总会要求他详细点，然后可以了解更多，但是有时候详细也会带来很多不便，到目前为止，我看到WSDL的时候还是有点晕，呵呵呵呵，尤其是当其中的方法较多的情况下，简直能够让人崩溃。怎么办？访问这个地址吧：[http://localhost/axis2/services/StockQuoteService?xsd](http://localhost/axis2/services/StockQuoteService?xsd)

（tomcat在默认情况下的端口为8080，本人一向比较懒惰为了访问时少输入点东西在地址栏，我把端口改成了80）

xml 代码

01. &lt;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"&gt;
02. &lt;xs:element name="getPrice"&gt;
03. &lt;xs:complexType&gt;
04. &lt;xs:sequence&gt;
05. &lt;xs:element name="symbol" nillable="true" type="xs:string" /&gt;
06. xs:sequence&gt;
07. xs:complexType&gt;
08. xs:element&gt;
09. &lt;xs:element name="getPriceResponse"&gt;
10. &lt;xs:complexType&gt;
11. &lt;xs:sequence&gt;
12. &lt;xs:element name="return" nillable="true" type="xs:double" /&gt;
13. xs:sequence&gt;
14. xs:complexType&gt;
15. xs:element&gt;
16. &lt;xs:element name="update"&gt;
17. &lt;xs:complexType&gt;
18. &lt;xs:sequence&gt;
19. &lt;xs:element name="symbol" nillable="true" type="xs:string" /&gt;
20. &lt;xs:element name="price" nillable="true" type="xs:double" /&gt;
21. xs:sequence&gt;
22. xs:complexType&gt;
23. xs:element&gt;
24. xs:schema&gt;

这些信息已经足够满足我们编写客户端程序的需要了。其中的**symbol**是我们在调用**getPrice**过程中需要传入的参数，而**return**是返回的参数。由于**update**方法不需要返回值，因此只有传入的参数**symbol**和**price**。这些信息是我们在下面的操作中都需要用到的。

 

不过在这个例子中，我们甚至根本就不需要编写客户端程序。仅仅通过访问就可以执行服务提供的接口。

 

访问[http://localhost/axis2/rest/StockQuoteService/getPrice?symbol=danlley](http://localhost/axis2/rest/StockQuoteService/getPrice?symbol=danlley)

xml 代码

1. &lt;ns:getPriceResponse xmlns:ns="http://quickstart.samples/xsd"&gt;
2. &lt;ns:return&gt;42.0ns:return&gt;
3. ns:getPriceResponse&gt;

这个是我们的程序在没有任何数据的情况下返回的默认值。

 

 

 

接下来让我们来更新数据：

首先在IE中浏览地址[http://localhost/axis2/rest/StockQuoteService/update?symbol=danlley&price=100](http://localhost/axis2/rest/StockQuoteService/update?symbol=danlley&amp%3Bprice=100)

 

你会发现执行完后没有任何反应，没关系让我们继续回过来在访问地址[http://localhost/axis2/rest/StockQuoteService/getPrice?symbol=danlley](http://localhost/axis2/rest/StockQuoteService/getPrice?symbol=danlley)

xml 代码

1. &lt;ns:getPriceResponse xmlns:ns="http://quickstart.samples/xsd"&gt;
2. &lt;ns:return&gt;100.0ns:return&gt;
3. ns:getPriceResponse&gt;

是不是数据已经成功得到修改了？！