//java serviceimport java.util.Date;public class SimpleService {public String getGreeting(String name) {return "你好 " + name+",現在時間是:"+new Date();}public int getPrice() {return new java.util.Random().nextInt(1000);}}
放入:%TOMCAT_HOME%\webapps\axis2\WEB-INF\pojo下,若沒有pojo,則建立一個pojo目錄.java client 法1:
package test;import javax.xml.namespace.QName;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;public class TestSimpleService {/** * @param args */public static void main(String[] args) throws Throwable{ // 使用RPC方式調用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調用WebService的URL EndpointReference targetEPR = new EndpointReference( "http://localhost:7777/axis2/services/SimpleService"); options.setTo(targetEPR); // 指定getGreeting方法的參數值 Object[] opAddEntryArgs = new Object[] {"超人"}; // 指定getGreeting方法傳回值的資料類型的Class對象 Class[] classes = new Class[] {String.class}; // 指定要調用的getGreeting方法及WSDL檔案的命名空間 QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting"); // 調用getGreeting方法並輸出該方法的傳回值 System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]); // 下面是調用getPrice方法的代碼,這些代碼與調用getGreeting方法的代碼類似 classes = new Class[] {int.class}; opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice"); System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);}}
java法2:
在Windows控制台輸出如下的命令列來產生調用WebService的代碼:
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub
其中-url參數指定了wsdl檔案的路徑,可以是本地路徑,也可以是網路路徑。-p參數指定了產生的Java類的包名,-o參數指定了產生的一系列檔案儲存的根目錄。在執行完上面的命令後,讀者就會發現在目前的目錄下多了個stub目錄,在."stub"src"client目錄可以找到一個SimpleServiceStub.java檔案,該檔案複雜調用WebService,讀者可以在程式中直接使用這個類,代碼如下:
package test;import client.SimpleServiceStub;public class TestSimpleServiceWithClientStub {/** * @param args */public static void main(String[] args) throws Throwable {SimpleServiceStub stub = new SimpleServiceStub();SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();gg.setName("一鬨而散在");String sGreetingResult = stub.getGreeting(gg).get_return();System.out.println(sGreetingResult);int iPriceResult = stub.getPrice().get_return();System.out.println(iPriceResult);}}
.net client 法3:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestAxis2ConsoleApp{ class Program { static void Main(string[] args) { //ServiceReference1.SimpleServicePortTypeClient client = new TestAxis2ConsoleApp.ServiceReference1.SimpleServicePortTypeClient("SimpleServiceHttpSoap11Endpoint"); ServiceReference1.SimpleServicePortTypeClient client = new TestAxis2ConsoleApp.ServiceReference1.SimpleServicePortTypeClient("SimpleServiceHttpSoap12Endpoint"); string s= client.getGreeting("吳xx"); Console.WriteLine(s); Console.WriteLine(); int price =client.getPrice(); Console.WriteLine(price); Console.ReadKey(); } }}