webservice 客戶端調用

現在我們來看xfire的客戶端調用,有兩種方式:

一、通過服務端提供的接口類進行調用。

Java代碼 複製代碼
  1. package com.wujianjun.xfire.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.util.List;  
  5.   
  6. import org.codehaus.xfire.XFire;  
  7. import org.codehaus.xfire.XFireFactory;  
  8. import org.codehaus.xfire.client.XFireProxyFactory;  
  9. import org.codehaus.xfire.service.Service;  
  10. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  11.   
  12. import com.wujianjun.xfire.domain.Person;  
  13. import com.wujianjun.xfire.spring.IPersonService;  
  14.   
  15. public class PojoInvokeClient {  
  16.   
  17.     public static void main(String[] args) {  
  18.         Service serviceModel = new ObjectServiceFactory().create(IPersonService.class);  
  19.   
  20.         XFire xfire = XFireFactory.newInstance().getXFire();  
  21.         XFireProxyFactory factory = new XFireProxyFactory(xfire);  
  22.         String serviceUrl = "http://127.0.0.1:8080/xfire/services/PersonService";  
  23.   
  24.         IPersonService client = null;  
  25.         try {  
  26.             client = (IPersonService) factory.create(serviceModel, serviceUrl);  
  27.         } catch (MalformedURLException e) {  
  28.             System.out.println("Client call webservice has exception: "+ e.toString());  
  29.         }  
  30.   
  31.         String result1 =client.sayHello("張三");  
  32.           
  33.     }  

 二、直接通過url調用, 不用客戶端提供接口類

Java代碼 複製代碼
  1. package com.wujianjun.xfire.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import org.codehaus.xfire.client.Client;  
  7.   
  8. public class UrlInvokeClient {  
  9.   
  10.     public static void main(String[] args) {  
  11.         Client client = null;  
  12.         try {  
  13.             client = new Client(new URL("http://127.0.0.1:8080/xfire/PersonService.ws?wsdl"));  
  14.             Object[] result1 = client.invoke("sayHello"new Object[] {"張三"});  
  15.             System.out.println(result1[0]);  
  16.         } catch (MalformedURLException e) {  
  17.             e.printStackTrace();  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  

發佈了38 篇原創文章 · 獲贊 8 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章