今天參考網上的文檔學習了一下使用Xfire構建webservice,相比較 Axis 而言,真有點相見很晚的感覺,使用Xfire的確簡單和方便不少,而且還能與spring整合使用,更難能可貴的是,Myeclipse官方網站還有關於使用Xfire的教程,地址如下:
http://www.myeclipseide.com/documentation/quickstarts/webservices/
可見Xfire的易用性是得到了公認的,具體的webservice開發上面的網址已經說的很清楚,這裡不累述了,只提一下用戶端的開發就行了。
使用XFire開發Web Service用戶端主要有兩種方式
一、服務提供者告訴你interface
- package test;
- import java.net.MalformedURLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
- public class WSClinet {
- public static void main(String[] args) {
- Service srvcModel = new ObjectServiceFactory()
- .create(IHelloService.class);
- XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
- .newInstance().getXFire());
- String helloWorldURL = "http://localhost:8080/wwwroot/services/HelloService";
- try {
- IHelloService srvc = (IHelloService) factory.create(srvcModel,
- helloWorldURL);
- System.out.println(srvc.sayHello("Robin"));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- }
二、通過WSDL建立一個動態用戶端
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.codehaus.xfire.client.Client;
- public class WSClinet {
- public static void main(String[] args) throws MalformedURLException,
- Exception {
- Client client = new Client(new URL(
- "http://www.webservicex.net/globalweather.asmx?WSDL"));
- Object[] results = client
- .invoke("GetCitiesByCountry", new Object[] { "China" });
- System.out.println(results[0]);
- }
- }
這裡需要知道webservice的操作方法以及輸入的參數,如GetCitiesByCountry,這個怎麼知道呢,開啟WSDL檔案,只看schema部分的代碼就很容易找到服務名及其參數定義了。
這兩種方法個人覺得動態方式要符合實際的應用一些,畢竟很多第三方是不會告訴你介面類的,感覺Myeclipse利用Xfire產生WS用戶端的工具也應該用的動態原理,而且還相當的方便,在main方法中就只關注operator name了。