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文件的命名空间名,也就是元素的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格局。通过工具给家展示下,发送的报文格式
SendSMS10001
1.0
I****AP
2013-12-04T14:19:47.0Z
*****
10001
send********i
0
1
ID*******tem
201312041419351034756432
1034
*****