在eclipse中使用jax-ws構建webservices服務端和用戶端

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   檔案   

服務端:
package com.yinfu.service;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebService  public class TestWebsService {    public String sayHello(String username) {        return "Hello: " + username;    }        public static void main(String[] args) {        Endpoint.publish("http://localhost:8075/com.yinfu.service.TestWebsService", new TestWebsService());        System.out.println("Success");    }}

 

在想要發布為WebService的類上加上註解@WebService,這個類的方法就變為WebService的方法了,再通過Endpoint的publish方法,發布這個服務,到此,一個最簡單的WebService搞定。運行main方法,在瀏覽器裡輸入”http://localhost:8075/com.yinfu.service.TestWebsService?wsdl “ 會看到你的WSDL資訊。

OK,說明服務端沒有問題,搭建成功

用戶端:

在命令列輸入命令 wsimport -p [包名] -keep [發布的服務地址?wsdl] 產生用戶端代碼,如產生本例的用戶端代碼wsimport -p com.yinfu.service.client-keep http://localhost:8075/com.yinfu.service.TestWebsService?wsdl“,當然,前提是你已經配好了JAVA環境變數。控制台會顯示

注意現在需要找到產生的用戶端java代碼,控制台指向在那產生的程式碼就在哪裡,例如我控制台指向的是C:\Users\Administrator\那麼我產生的java檔案就在C:\Users\Administrator\ com\yinfu\service\client\中,這個地方注意一下就可以了,有很多人不知道產生的程式碼去哪裡了!

OK,把產生的程式碼拷貝到用戶端的項目中,

package com.yinfu.service.client;public class HelloClient {    /**     * @param args     */    public static void main(String[] args) {        TestWebsServiceService myService = new TestWebsServiceService();        TestWebsService ms = myService.getTestWebsServicePort();        String s = ms.sayHello("why");        System.out.println(s);    }}

 

利用這些產生的用戶端代碼,就可以調用這個WebService服務了:執行代碼,輸出:Hello why。啟動並執行時候要注意服務端的專案服務是啟動的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.