原文地址:http://os.ajava.org/category/xml/axis2-web%E6%9C%8D%E5%8A%A1%E5%B7%A5%E5%85%B7%EF%BC%9Aj2xb-axis2/
一、J2XB Axis2介绍
J2XB Axis2模块允许使用J2XB注释创建具有自动生成WSDL的Axis2 Web服务,模块使用了WS - RPC样式,每个方法映射为Web服务操作,方法的参数映射为输入的信息,方法的结果映射为输出的信息和例外错误信息。
Axis2使J2XB XML schema功能得到最大的体现;Axis2允许让WSDL包含其他事物,如约束面、置换组等。
二、创建一个J2XB Axis2 Web服务
要创建一个J2XB Axis2 Web服务,只需要创建一个POJO类(或接口)作为Web服务。参数的返回类型和违例可以使用这个类与J2XB映射 。部署为一个标准的Axis2服务,AAR文档(不含WSDL文件的AAR)包含services.xml文件和服务类。
下面是使用J2XB Axis2 module的标准Axis2服务的例子。
三、安装J2XB Axis2
Axis2 web application目录如下:
axis2
+-- axis2-web
+-- META-INF
+-- WEB-INF
+-- classes
+-- conf
+-- lib
+-- modules
| +-- modules.list
| +--
+-- services
| +-- services.list
| +--
+-- web.xml
安装J2XB Axis2 module需要J2XB 1.1+版本,需要JDK 1.5及以上版本
安装步骤如下:
1.下载J2XB 1.1+
2.下载J2XB Axis2 module
3.将* j2xb-core-*.jar * j2xb-axiom-binding-*.jar文件复制到axis2/WEB-INF/lib目录下。
4.将* j2xb-axis2-*.mar文件复制到axis2/WEB-INF/modules目录下。
5.某些时候,你可能需要在axis2/WEB-INF/modules/modules.list里添加j2xb-axis2-*.mar的定义。
四、J2XB Axis2 Web服务例子
1.服务执行类
服务执行类基本与Axis2例子的相同,并具有部分J2XB的违例注释。
package samples.quickstart.service.com;
public class StockQuoteService {
private HashMap map = new HashMap();
@MOValidationNumber(minInclusive = "0")
public double getPrice(
@MOValidationString(minLength = 2, maxLength = 7, patterns = ("[A-Z0-9.]+"))
String symbol) {
Double price = map.get(symbol);
if(price != null){
return price;
}
return 42.00;
}
public void update(
@MOValidationString(minLength = 2, maxLength = 7, patterns = ("[A-Z0-9.]+"))
String symbol,
@MOValidationNumber(minInclusive = "0")
double price) {
map.put(symbol, price);
}
}
2.部署服务
为了部署服务,我们需要打包一个标准的Axis2 aar文档,文档结构如下:
quick-start-1.0.0.aar
+-- META-INF
| +-- services.xml
+-- samples
+-- quickstart
+-- service
+-- pojo
+-- StockQuoteService.class
需要注意的是,这里没有WSDL文件,它是由J2XB自动生成的。
一个标准Axis2 service 的services.xml文件如下:
<service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/ ">
<description>
Stock Quote Service
</description>
<module ref="J2XB"/>
<schema schemaNamespace="http://quickstart.samples/xsd"/ >
<parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteService</parameter>
</service>
需要注意的是,services.xml不包含messageReceiver的定义,这些没有必要作为J2XB模块,messageReceiver将使用自己拥有的信息接收器(参数的排列使用J2XB)。
3.生成WSDL
下面是通过J2XB Axis2 Web服务生成WSDL的代码:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/ " xmlns:axis2="http://quickstart.samples/ " xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/ " xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/ " xmlns:http="http://schemas.xmlsoap.org/wsdl/http/ " xmlns:ns1="http://org.apache.axis2/xsd " xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl " xmlns:ns="http://quickstart.samples/xsd " xmlns:xs="http://www.w3.org/2001/XMLSchema " xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/ " targetNamespace="http://quickstart.samples/ ">
<wsdl:documentation>StockQuoteService</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd ">
<xs:element name="getPrice">
<xs:complexType>
<xs:sequence>
<xs:element name="symbol">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="7"/>
<xs:pattern value="[p{Upper}.]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getPriceResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="return">
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="update">
<xs:complexType>
<xs:sequence>
<xs:element name="symbol">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="7"/>
<xs:pattern value="[p{Upper}.]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="price">
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="getPriceRequest">
<wsdl:part name="parameters" element="ns:getPrice"/>
</wsdl:message>
<wsdl:message name="getPriceResponse">
<wsdl:part name="parameters" element="ns:getPriceResponse"/>
</wsdl:message>
<wsdl:message name="updateRequest">
<wsdl:part name="parameters" element="ns:update"/>
</wsdl:message>
<wsdl:portType name="StockQuoteServicePortType">
<wsdl:operation name="getPrice">
<wsdl:input message="axis2:getPriceRequest" wsaw:Action="urn:getPrice"/>
<wsdl:output message="axis2:getPriceResponse" wsaw:Action="urn:getPriceResponse"/>
</wsdl:operation>
<wsdl:operation name="update">
<wsdl:input message="axis2:updateRequest" wsaw:Action="urn:update"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="StockQuoteServiceSoap11Binding" type="axis2:StockQuoteServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http " style="document"/>
<wsdl:operation name="getPrice">
<soap:operation soapAction="urn:getPrice" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="update">
<soap:operation soapAction="urn:update" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="StockQuoteServiceSoap12Binding" type="axis2:StockQuoteServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http " style="document"/>
<wsdl:operation name="getPrice">
<soap12:operation soapAction="urn:getPrice" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="update">
<soap12:operation soapAction="urn:update" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="StockQuoteServiceHttpBinding" type="axis2:StockQuoteServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="getPrice">
<http:operation location="StockQuoteService/getPrice"/>
<wsdl:input>
<mime:content type="text/xml" part="getPrice"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="getPrice"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="update">
<http:operation location="StockQuoteService/update"/>
<wsdl:input>
<mime:content type="text/xml" part="update"/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="StockQuoteService">
<wsdl:port name="StockQuoteServiceHttpSoap11Endpoint" binding="axis2:StockQuoteServiceSoap11Binding">
<soap:address location="http://10.234.10.148:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpSoap11Endpoint"/ >
</wsdl:port>
<wsdl:port name="StockQuoteServiceHttpSoap12Endpoint" binding="axis2:StockQuoteServiceSoap12Binding">
<soap12:address location="http://10.234.10.148:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpSoap12Endpoint"/ >
</wsdl:port>
<wsdl:port name="StockQuoteServiceHttpEndpoint" binding="axis2:StockQuoteServiceHttpBinding">
<http:address location="http://10.234.10.148:8080/axis2/services/StockQuoteService.StockQuoteServiceHttpEndpoint"/ >
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
需要注意的是,wsdl:types部分包含产生XML Schema,包含J2XB注释定义的约束。
五、下载
官方下载地址:http://sourceforge.net/project/showfiles.php?group_id=228363
六、参考资料:
主站地址:http://j2xb.sourceforge.net/index.html
API文档地址:http://j2xb.sourceforge.net/apidocs/index.html
七、结束语
本文对J2XB Axis2作了简单的介绍,由于本人水平有限,如发现有错误纰漏,请指正。
联系方式:
网站:http://ajava.org
QQ:76662116
EM:chinesedocument@gamil.com
八、作者简介
mark,ajava.org站长,软件工程师,从事多年软件开发,曾开发多个市级、省级、国家级软件。