您的位置:首页 > 编程语言 > Java开发

java RPC方式调用axis2 webservice

2014-12-26 10:48 435 查看
url: /article/5681134.html

所需jar包包括:

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


除了引入的这些jar包之外,还需要axis2 下的commons-httpclient-3.1.jar包,通过该包进行webservice通信。

调用axis2服务和调用wcf服务不同,所需要传入的参数包括,webservice地址,方法名,方法参数,webservice命名空间,和返回类型,其中方法参数是一个Object数组,参数类型根据Object数组决定。源码如下:

private static  RPCServiceClient serviceClient;
/**
* RPC调用AXIS2 webservice
* @param endpoint 服务地址 如:http://192.168.0.1:2597/aixs2/services/jqservice?wsdl
* @param localPart 方法名 如<xs:element name="Receive">
* @param opArgs 方法参数 如Object[] opArgs = new Object[] { param };
* @param namespaceURI 命名空间 如 :targetNamespace="http://server.test.com.cn">
* @param opReturnType 返回类型 如字符串:Class[] opReturnType = new Class[] { String[].class };
*/
public static String axis2RPCInvoke(String endpoint,String localPart,Object[] opArgs,String namespaceURI,Class[] opReturnType)
{
Object[] ret = null;
try
{
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
QName opQName = new QName(namespaceURI, localPart);
ret = serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
System.out.println(((String[]) ret[0])[0]);
}
catch (AxisFault e)
{
e.printStackTrace();
}
return ((String[]) ret[0])[0];
}


测试方法如下:

public static void main(String[] args)
{
axis2RPCInvoke("http://172.16.5.22:9090/axis2/services/MyService?wsdl", "Receive", new Object[] {"122"}, "http://lincb.com",  new Class[] { String[].class });
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: