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 });
        }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM