Axis2呼叫webservice Client
阿新 • • 發佈:2019-01-09
Axis2呼叫webservice,很多人和網上都使用的RPCServiceClient進行呼叫webservice,但是有時候並不是一定都滿足需求,接下來會展示下RPCServiceClient的簡單使用,和當無法滿足需求時候怎麼使用serviceClient。
首先展示下RPCServiceClient的呼叫示例:
package com.zjhcsoft.eddmp.webService.dao; 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.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import javax.xml.namespace.QName; /** * RPCServiceClient呼叫demo * User: Francis.Hu * Date: 13-12-4 * Time: 下午12:10 */ public class TestClass { public static void main(String[] args) { String url = "http://10.7.92.232:8080/SMS_Send_Service/services/smsSend"; String method = "send"; String serviceNameH = "SendSMS10***1"; String serviceVer = "1.**"; String consumer = "**"; String requestTime = "2001-12-17T09:30:47.0Z"; String destTermID = "133***1681"; String srcTermID = "10001"; String msgContent = "給***測試"; String disturbType = "0"; String needReport = "1"; String serviceName = "***系統"; String serialID = "201312041024351034756432"; String serviceProvider = "1034"; String pwd = "***"; try { String xml = null; RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = factory.createOMNamespace("http://www.shtel.com.cn/csb/v2/",""); //引數1名稱空間,引數2空間字首可為空 OMElement header = factory.createOMElement("CSBHeader",omNs); OMElement sn = factory.createOMElement("ServiceName", omNs); sn.setText(serviceNameH); header.addChild(sn); OMElement sv = factory.createOMElement("ServiceVer", omNs); sv.setText(serviceVer); header.addChild(sv); OMElement cs = factory.createOMElement("Consumer", omNs); cs.setText(consumer); header.addChild(cs); OMElement rt = factory.createOMElement("RequestTime", omNs); rt.setText(requestTime); header.addChild(rt); //設定頭部資訊,如果大家不需要可以直接刪除 serviceClient.addHeader(header); EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // 在建立QName物件時,QName類的構造方法的第一個引數表示WSDL檔案的名稱空間名,也就是<wsdl:definitions>元素的targetNamespace屬性值 QName opAddEntry = new QName("http://www.shtel.com.cn/csb/v2/", method); // 引數,如果有多個,繼續往後面增加即可,不用指定引數的名稱,到時候會預設未arg0,arg1,arg2,arg3.... Object[] opAddEntryArgs = new Object[]{destTermID, srcTermID, msgContent, disturbType, needReport, serviceName, serialID, serviceProvider, pwd}; /*invokeBlocking方法有三個引數,其中第一個引數的型別是QName物件,表示要呼叫的方法名; 第二個引數表示要呼叫的WebService方法的引數值,引數型別為Object[]; 第三個引數表示WebService方法的返回值型別的Class物件,引數型別為Class[]。 當方法沒有引數時,invokeBlocking方法的第二個引數值不能是null,而要使用new Object[]{} 如果被呼叫的WebService方法沒有返回值,應使用RPCServiceClient類的invokeRobust方法, 該方法只有兩個引數,它們的含義與invokeBlocking方法的前兩個引數的含義相同*/ Class[] classes = new Class[]{Object.class}; xml = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; System.out.println(xml); } catch (AxisFault axisFault) { axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }
上面的程式碼展示了基於RPCServiceClien進行呼叫,但是傳入引數的時候無法指定引數鍵值(個人沒有找到設定的方法,如果有朋友找到可以和我分享下,萬分感謝),RPCServiceClient繼承於父類serviceClient,對父類進行拓展,我們可以直接拿著serviceClient進行操作實現我們的需求,定製xml格式,繼續貼上程式碼
package com.zjhcsoft.eddmp.webService.dao; 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.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; /** * 使用serviceClient進行xml定製傳送 * User: Francis.Hu * Date: 13-12-4 * Time: 下午1:41 */ public class BaseUtil { public static void main(String[] args) { String url = "http://10.7.92.232:8080/SMS_Send_Service/services/smsSend"; String serviceNameH = "SendSMS10001"; String serviceVer = "1.0"; String consumer = "********"; String requestTime = "2013-12-04T14:19:47.0Z"; String destTermID = "*********"; String srcTermID = "10001"; String msgContent = "sen****i"; String disturbType = "0"; String needReport = "1"; String serviceName = "****
"; String serialID = "201312041419351034756432"; String serviceProvider = "*****"; String pwd = "****"; try { Options options = new Options(); // 指定呼叫WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); //client ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = factory.createOMNamespace("http://www.shtel.com.cn/csb/v2/",""); //頭訊息指定 OMElement header = factory.createOMElement("CSBHeader",omNs); OMElement sn = factory.createOMElement("ServiceName", omNs); sn.setText(serviceNameH); header.addChild(sn); OMElement sv = factory.createOMElement("ServiceVer", omNs); sv.setText(serviceVer); header.addChild(sv); OMElement cs = factory.createOMElement("Consumer", omNs); cs.setText(consumer); header.addChild(cs); OMElement rt = factory.createOMElement("RequestTime", omNs); rt.setText(requestTime); header.addChild(rt); sender.addHeader(header); //方法的定義,namespace和後面m0的效果,參考下面報文示例 OMNamespace bodyNM = factory.createOMNamespace("http://webservice.ideal.com","m0"); OMElement method = factory.createOMElement("send", bodyNM); //方法裡的引數1 OMElement destTermXML = factory.createOMElement("destTermID", bodyNM); destTermXML.setText(destTermID); method.addChild(destTermXML); //方法裡的引數2,下面依次類推 OMElement srcTermIDXML = factory.createOMElement("srcTermID", bodyNM); srcTermIDXML.setText(srcTermID); method.addChild(srcTermIDXML); OMElement msgContentXML = factory.createOMElement("msgContent", bodyNM); msgContentXML.setText(msgContent); method.addChild(msgContentXML); OMElement disturbTypeXML = factory.createOMElement("disturbType", bodyNM); disturbTypeXML.setText(disturbType); method.addChild(disturbTypeXML); OMElement needReportXML = factory.createOMElement("needReport", bodyNM); needReportXML.setText(needReport); method.addChild(needReportXML); OMElement serviceNameXML = factory.createOMElement("serviceName", bodyNM); serviceNameXML.setText(serviceName); method.addChild(serviceNameXML); OMElement serialIDXML = factory.createOMElement("serialID", bodyNM); serialIDXML.setText(serialID); method.addChild(serialIDXML); OMElement serviceProviderXML = factory.createOMElement("serviceProvider", bodyNM); serviceProviderXML.setText(serviceProvider); method.addChild(serviceProviderXML); OMElement pwdXML = factory.createOMElement("pwd", bodyNM); pwdXML.setText(pwd); method.addChild(pwdXML); //構建request報文 method.build(); //傳送報文請求,返回OMElement OMElement result = sender.sendReceive(method); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }
上面就是serviceClient的使用,主要原理就是構建xml格局。通過工具給家展示下,傳送的報文格式
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<CSBHeader xmlns="http://www.shtel.com.cn/csb/v2/">
<ServiceName>SendSMS10001</ServiceName>
<ServiceVer>1.0</ServiceVer>
<Consumer>I****AP</Consumer>
<RequestTime>2013-12-04T14:19:47.0Z</RequestTime>
</CSBHeader>
</soapenv:Header>
<soapenv:Body>
<m0:send xmlns:m0="http://webservice.ideal.com">
<m0:destTermID>*****</m0:destTermID>
<m0:srcTermID>10001</m0:srcTermID>
<m0:msgContent>send********i</m0:msgContent>
<m0:disturbType>0</m0:disturbType>
<m0:needReport>1</m0:needReport>
<m0:serviceName>ID*******tem</m0:serviceName>
<m0:serialID>201312041419351034756432</m0:serialID>
<m0:serviceProvider>1034</m0:serviceProvider>
<m0:pwd>*****</m0:pwd>
</m0:send>
</soapenv:Body>
</soapenv:Envelope>
以上就是對Client的使用兩種方式,個人比較偏向這樣的客戶端呼叫,用工具根據wsdl生成的程式碼又多又亂,自己寫的就是好的^_^