原文出处:
袁鸣凯
一、SOAPIn Axis2
在前两天的教程中,我们学习到了用Axis2如何进行复杂数据、简单数据进行传输。
正如我在前一天教程中所说,在web service的世界里,一切都是基于SOAP的,因此在今天我们将学习Axis2中的SOAP特性。
今天的课程将用3个例子来完成即:
- 客户端与服务端使用SOAP进行通讯
- 服务端将Exception以SOAPFault的形式抛给客户端
- 使用SWA(Soap With Attachment)来进行附件传送
二、客户端与服务端使用SOAP进行通讯
来看下面这个Web Service:
下面是Service端的源码
org.sky.axis2.soap.SoapService
package org.sky.axis2.soap; 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 java.util.*; public class SoapService { public static OMElement requestSoap = null; public OMElement request(OMElement soapBody) { requestSoap = soapBody; Iterator it = requestSoap.getChildElements(); OMElement issuerElement = (OMElement) it.next(); OMElement serialElement = (OMElement) it.next(); OMElement revocationDateElement = (OMElement) it.next(); String issuer = issuerElement.getText(); String serial = serialElement.getText(); String revocationDate = revocationDateElement.getText(); System.out.println("issuer=====" + issuer); System.out.println("serial=====" + serial); System.out.println("revocationDate=====" + revocationDate); OMFactory soapFactory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = soapFactory.createOMNamespace( "http://soap.axis2.sky.org", ""); OMElement soapResponse = soapFactory.createOMElement("SoapResponse", omNs); OMElement soapIssuer = soapFactory.createOMElement("Issuer", omNs); soapIssuer.setText("issuer: " + issuer); soapResponse.addChild(soapIssuer); OMElement soapSerial = soapFactory.createOMElement("Serial", omNs); soapSerial.setText("serial: " + serial); soapResponse.addChild(soapSerial); OMElement soapRevokeDate = soapFactory.createOMElement("RevokeDate", omNs); soapRevokeDate.setText("RevocationDate: " + revocationDate); soapResponse.addChild(soapRevokeDate); soapResponse.build(); return soapResponse; } }
来看它的service.xml的描述
<service name="SoapService"> <description> This is the service for revoking certificate. </description> <parameter name="ServiceClass" locked="false"> org.sky.axis2.soap.SoapService </parameter> <operation name="request"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" /> <actionMapping>urn:request</actionMapping> </operation> </service>
该Web Service接受一个Soap请求,该请求为如下格式:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.axis2.sky.org"> <soapenv:Header/> <soapenv:Body> <soap:request> <soap:request>?</soap:request> </soap:request> </soapenv:Body> </soapenv:Envelope>
其中