開發基於Xfire的Webservice

來源:互聯網
上載者:User

今天參考網上的文檔學習了一下使用Xfire構建webservice,相比較 Axis 而言,真有點相見很晚的感覺,使用Xfire的確簡單和方便不少,而且還能與spring整合使用,更難能可貴的是,Myeclipse官方網站還有關於使用Xfire的教程,地址如下:

 

http://www.myeclipseide.com/documentation/quickstarts/webservices/

 

可見Xfire的易用性是得到了公認的,具體的webservice開發上面的網址已經說的很清楚,這裡不累述了,只提一下用戶端的開發就行了。

 

使用XFire開發Web Service用戶端主要有兩種方式

 

一、服務提供者告訴你interface

  1. package test;
  2. import java.net.MalformedURLException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.codehaus.xfire.XFireFactory;
  6. import org.codehaus.xfire.client.XFireProxyFactory;
  7. import org.codehaus.xfire.service.Service;
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;
  9. public class WSClinet {
  10.     public static void main(String[] args) {
  11.         Service srvcModel = new ObjectServiceFactory()
  12.                 .create(IHelloService.class);
  13.         XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
  14.                 .newInstance().getXFire());
  15.         String helloWorldURL = "http://localhost:8080/wwwroot/services/HelloService";
  16.         try {
  17.             IHelloService srvc = (IHelloService) factory.create(srvcModel,
  18.                     helloWorldURL);
  19.             System.out.println(srvc.sayHello("Robin"));
  20.         } catch (MalformedURLException e) {
  21.             e.printStackTrace();
  22.         }
  23.     }
  24. }

二、通過WSDL建立一個動態用戶端

  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import org.codehaus.xfire.client.Client;
  4. public class WSClinet {
  5.     public static void main(String[] args) throws MalformedURLException,
  6.             Exception {
  7.         Client client = new Client(new URL(
  8.                 "http://www.webservicex.net/globalweather.asmx?WSDL"));
  9.         Object[] results = client
  10.                 .invoke("GetCitiesByCountry", new Object[] { "China" });
  11.         System.out.println(results[0]);
  12.     }
  13. }

這裡需要知道webservice的操作方法以及輸入的參數,如GetCitiesByCountry,這個怎麼知道呢,開啟WSDL檔案,只看schema部分的代碼就很容易找到服務名及其參數定義了。

 

這兩種方法個人覺得動態方式要符合實際的應用一些,畢竟很多第三方是不會告訴你介面類的,感覺Myeclipse利用Xfire產生WS用戶端的工具也應該用的動態原理,而且還相當的方便,在main方法中就只關注operator name了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.