<soap:request></soap:request>中间的内容,应该如下所示:
<Request xmlns="http://10.225.104.122">
<Issuer>1234567890</Issuer>
<Serial>11111111</Serial>
<RevokeDate>2007-01-01</RevokeDate>
</ Response >
我们假设它是一个购买图书的定单,服务端收到这个请求后会返回一个定单信息给调用它的客户端,服务端将返回如下内容(此处不做任何业务处理,只是很简单的传值回客户端)。
<SoapResponse xmlns="http://soap.axis2.sky.org">
<Issuer>issuer: Wrox</Issuer>
<Serial>serial: 1111111111ISBN</Serial>
<RevokeDate>RevocationDate: 2012-07-29</RevokeDate>
</SoapResponse>
为生成上述这个SoapResponse我们在Service端的核心代码如上面加粗部分的代码所示,由其注意这个“soapResponse.build();”。
下面我们来看这个客户端是怎么写的,我们这边用的是非阻塞式客户端
org.sky.axis2.soap.SoapServiceClient
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 org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.async.AxisCallback;
import org.apache.axis2.context.MessageContext;
import javax.xml.namespace.QName;
public class SoapServiceClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/Axis2Service/services/SoapService");
public static boolean finish = false;
public static void orderRequest() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace omNs = factory.createOMNamespace(
"http://soap.axis2.sky.org", "");
OMElement issuer = factory.createOMElement("Issuer", omNs);
OMElement serial = factory.createOMElement("Serial", omNs);
OMElement revocationDate = factory.createOMElement("RevocationDate",
omNs);
issuer.setText("Wrox");
serial.setText("1111111111ISBN");
revocationDate.setText("2012-07-29");
OMElement requestSoapMessage = factory.createOMElement("request", omNs);
requestSoapMessage.addChild(issuer);
requestSoapMessage.addChild(serial);
requestSoapMessage.addChild(revocationDate);
requestSoapMessage.build();
Options options = new Options();
options.setTo(targetEPR);
ServiceClient sender = null;
try {
AxisCallback callback = new AxisCallback() {
public void onMessage(MessageContext msgContext) {
OMElement result = msgContext.getEnvelope().getBody()
.getFirstElement();
// System.out.println(msgContext.toString());
// System.out.println(msgContext.getEnvelope().toString());
System.out.println(msgContext.getEnvelope().getBody()
.getFirstElement());
finish = true;
}
public void onFault(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody()
.getFault().toString());
}
public void onError(Exception e) {
}
public void onComplete() {