通向架构师的道路 第十二天 Axis2 Web Service(三)2013-02-15 csdn lifetragedy一、SOAPIn Axis2在前两天的教程中,我们学习到了用Axis2如何进行复杂数据、简单数据进行传输。正如我在前一天教程中所说,在web service的世界里,一切都是基于SOAP的,因此在今天我们将学习Axis2中的SOAP特性。今天的课程将用3个例子来完成即:1)客户端与服务端使用SOAP进行通讯2)服务端将Exception以SOAPFault的形式抛给客户端3)使用SWA(Soap With Attachment)来进行附件传送二、客户端与服务端使用SOAP进行通讯来看下面这个Web Service:

下面是Service端的源码
org.sky.axis2.soap.SoapServicepackage 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;
}
}