(1)使用RPC方式調用WebService
// 使用RPC方式調用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調用WebService的URL EndpointReference targetEPR = new EndpointReference("http://localhost:8085/axisDemo/services/Calculate?wsdl"); options.setTo(targetEPR); // 指定add方法的參數值 Object[] opAddEntryArgs = new Object[] {1,2}; // 指定Integer方法返回值的數據類型的Class對象 Class[] classes = new Class[] {Integer.class}; // 指定要調用的add方法及WSDL文件的命名空間 QName opAddEntry = new QName("http://test.com", "add"); /** * 調用add方法並輸出該方法的返回值 * invokeBlocking方法有三個參數,其中第一個參數的類型是QName對象, * 表示要調用的方法名;第二個參數表示要調用的WebService方法的參數值, * 參數類型為Object[]; 第三個參數表示WebService方法的返回值類型的Class對象, * 參數類型為Class[],當方法沒有參數時,invokeBlocking方法的第二個參數值不能是null, * 而要使用new Object[]{} */ int result=(Integer) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; //如果被調用的WebService方法沒有返回值,應使用RPCServiceClient類的invokeRobust方法, //該方法只有兩個參數,它們的含義與invokeBlocking方法的前兩個參數的含義相同 // serviceClient.invokeRobust(opName, opAddEntryArgs); System.out.println(result);
(2)用wsdl2java命令的方式生成代碼
wsdl2java -uri http://localhost:8085/axisDemo/services/Calculate?wsdl -p client -s -o stub
其中-uri參數指定了wsdl文件的路徑
-p參數指定了生成的Java類的包名,
-o參數指定了生成的一系列文件保存的根目錄。