通向架构师的道路第十二天之Axis2 Web Service三文档格式.docx
- 文档编号:21095072
- 上传时间:2023-01-27
- 格式:DOCX
- 页数:24
- 大小:231.45KB
通向架构师的道路第十二天之Axis2 Web Service三文档格式.docx
《通向架构师的道路第十二天之Axis2 Web Service三文档格式.docx》由会员分享,可在线阅读,更多相关《通向架构师的道路第十二天之Axis2 Web Service三文档格式.docx(24页珍藏版)》请在冰豆网上搜索。
publicclassSoapService{
publicstaticOMElementrequestSoap=null;
publicOMElementrequest(OMElementsoapBody){
requestSoap=soapBody;
Iteratorit=requestSoap.getChildElements();
OMElementissuerElement=(OMElement)it.next();
OMElementserialElement=(OMElement)it.next();
OMElementrevocationDateElement=(OMElement)it.next();
Stringissuer=issuerElement.getText();
Stringserial=serialElement.getText();
StringrevocationDate=revocationDateElement.getText();
System.out.println("
issuer====="
+issuer);
serial====="
+serial);
revocationDate====="
+revocationDate);
OMFactorysoapFactory=OMAbstractFactory.getOMFactory();
OMNamespaceomNs=soapFactory.createOMNamespace(
"
http:
//soap.axis2.sky.org"
"
"
);
OMElementsoapResponse=soapFactory.createOMElement("
SoapResponse"
omNs);
OMElementsoapIssuer=soapFactory.createOMElement("
Issuer"
omNs);
soapIssuer.setText("
issuer:
soapResponse.addChild(soapIssuer);
OMElementsoapSerial=soapFactory.createOMElement("
Serial"
soapSerial.setText("
serial:
soapResponse.addChild(soapSerial);
OMElementsoapRevokeDate=soapFactory.createOMElement("
RevokeDate"
soapRevokeDate.setText("
RevocationDate:
soapResponse.addChild(soapRevokeDate);
soapResponse.build();
returnsoapResponse;
}
}
来看它的service.xml的描述
<
servicename="
SoapService"
>
<
description>
Thisistheserviceforrevokingcertificate.
/description>
parametername="
ServiceClass"
locked="
false"
org.sky.axis2.soap.SoapService
/parameter>
operationname="
request"
messageReceiver
class="
org.apache.axis2.receivers.RawXMLINOutMessageReceiver"
/>
actionMapping>
urn:
request<
/actionMapping>
/operation>
/service>
该WebService接受一个Soap请求,该请求为如下格式:
soapenv:
Envelopexmlns:
soapenv="
//schemas.xmlsoap.org/soap/envelope/"
xmlns:
soap="
Header/>
Body>
soap:
request>
?
/soap:
/soapenv:
Envelope>
其中<
中间的内容,应该如下所示:
Requestxmlns="
//10.225.104.122"
Issuer>
1234567890<
/Issuer>
Serial>
11111111<
/Serial>
RevokeDate>
2007-01-01<
/RevokeDate>
/Response>
我们假设它是一个购买图书的定单,服务端收到这个请求后会返回一个定单信息给调用它的客户端,服务端将返回如下内容(此处不做任何业务处理,只是很简单的传值回客户端)。
SoapResponsexmlns="
Wrox<
1111111111ISBN<
2012-07-29<
/SoapResponse>
为生成上述这个SoapResponse我们在Service端的核心代码如上面加粗部分的代码所示,由其注意这个“soapResponse.build();
”。
下面我们来看这个客户端是怎么写的,我们这边用的是非阻塞式客户端
org.sky.axis2.soap.SoapServiceClient
importorg.apache.axis2.AxisFault;
importorg.apache.axis2.Constants;
importorg.apache.axis2.addressing.EndpointReference;
importorg.apache.axis2.client.Options;
importorg.apache.axis2.client.ServiceClient;
importorg.apache.axis2.client.async.AxisCallback;
importorg.apache.axis2.context.MessageContext;
importjavax.xml.namespace.QName;
publicclassSoapServiceClient{
privatestaticEndpointReferencetargetEPR=newEndpointReference(
//localhost:
8080/Axis2Service/services/SoapService"
publicstaticbooleanfinish=false;
publicstaticvoidorderRequest(){
OMFactoryfactory=OMAbstractFactory.getOMFactory();
OMNamespaceomNs=factory.createOMNamespace(
OMElementissuer=factory.createOMElement("
OMElementserial=factory.createOMElement("
OMElementrevocationDate=factory.createOMElement("
RevocationDate"
omNs);
issuer.setText("
Wrox"
serial.setText("
1111111111ISBN"
revocationDate.setText("
2012-07-29"
OMElementrequestSoapMessage=factory.createOMElement("
requestSoapMessage.addChild(issuer);
requestSoapMessage.addChild(serial);
requestSoapMessage.addChild(revocationDate);
requestSoapMessage.build();
Optionsoptions=newOptions();
options.setTo(targetEPR);
ServiceClientsender=null;
try{
AxisCallbackcallback=newAxisCallback(){
publicvoidonMessage(MessageContextmsgContext){
OMElementresult=msgContext.getEnvelope().getBody()
.getFirstElement();
//System.out.println(msgContext.toString());
//System.out.println(msgContext.getEnvelope().toString());
System.out.println(msgContext.getEnvelope().getBody()
.getFirstElement());
finish=true;
publicvoidonFault(MessageContextmsgContext){
System.out.println(msgContext.getEnvelope().getBody()
.getFault().toString());
publicvoidonError(Exceptione){
publicvoidonComplete(){
Completed!
!
};
sender=newServiceClient();
sender.setOptions(options);
-------Invoketheservice---------"
sender.sendReceiveNonBlocking(requestSoapMessage,callback);
synchronized(callback){
if(!
finish){
callback.wait(1000);
}catch(Exceptione){
thrownewAxisFault(
Serverwasshutdownastheasyncresponsetaketoolongtocomplete"
}catch(AxisFaulte){
e.printStackTrace();
}finally{
if(sender!
=null)
sender.cleanup();
publicstaticvoidmain(String[]args){
orderRequest();
上述代码和前两天的客户端代码没啥区别,我已经把核心代码用红色给标粗了。
运行后行得到输出
客户端运行后的输出:
服务端的输出:
三、服务端将Exception以SOAPFault的形式抛给客户端
上面这个例子很简单,它展示了一个客户端向服务端发送一个request,服务端接收到客户端的Request(OMElement类型)后解析并根据相应的业务逻辑向客户端再返回一个response(OMElement类型)的完整过程。
下面我们要来看的是,如果客户端在调用服务器时发生任何错误,服务端如何把这个错误经过包装后再返回给客户端的例子。
还记得我们在非阻塞式客户端中有如下这样的触发器吗?
publicvoidonMessage(MessageContextmsgContext){
publicvoidonFault(MessageContextmsgContext){
publicvoidonError(Exceptione){
publicvoidonComplete(){
此处的onFault就是用于接受从服务端抛过来的Exception的,我们把它称为SOAPFault。
下面来看一个例子,先来看Service端
org.sky.axis2.soap.SoapFaultService
importorg.apache.axiom.o
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 通向架构师的道路第十二天之Axis2 Web Service三 通向 架构 道路 第十二 Axis2 Service