java RPC方式调用axis2 webservice

  1. 所需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通信。

  2. 调用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];
        }

     

  3. 测试方法如下:
    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 });
        }

猜你喜欢

转载自xiaoshanjnby.iteye.com/blog/2219643