一、新建一個maven項目
二、pom.xml引入axis2依賴
<dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-http</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-local</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-kernel</artifactId> <version>1.6.2</version> </dependency>
三、編寫接口調用測試類
public static void main(String[] args) throws Exception { //本機tomcat端口默認為8080 EndpointReference targetEPR = new EndpointReference("http://localhost:8080/CxfWSServer/webservice/helloWorld"); RPCServiceClient sender = new RPCServiceClient(); Options options = sender.getOptions(); options.setTimeOutInMilliSeconds(2*20000L);//超時時間20s options.setTo(targetEPR); QName qname = new QName("http://ws.xie.com/", "sayHi"); String str = "April"; Object[] param = new Object[]{str}; Class<?>[] types = new Class[]{String.class}; //這是針對返值類型的 /** * RPCServiceClient類的invokeBlocking方法調用了WebService中的方法。 * invokeBlocking方法有三個參數 * 第一個參數的類型是QName對象,表示要調用的方法名; * 第二個參數表示要調用的WebService方法的參數值,參數類型為Object[]; * 第三個參數表示WebService方法的返回值類型的Class對象,參數類型為Class[]。 * * 當方法沒有參數時,invokeBlocking方法的第二個參數值不能是null,而要使用new Object[]{}。 */ Object[] response = sender.invokeBlocking(qname, param, types); System.out.println(response[0]); }
注意:代碼中標紅的地方分別對應如圖所示三個地方