WebService學習筆記-Hello world

來源:互聯網
上載者:User

標籤:webservice

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/27/wKiom1Q3dkWB8TjnAAEeii4Y_so241.jpg" title="QQ20141010140115.png" alt="wKiom1Q3dkWB8TjnAAEeii4Y_so241.jpg" />

中包含一個伺服器和一個用戶端。

伺服器端

HelloWS.java介面

/** * SEI * */@WebServicepublic interface HelloWS {@WebMethodpublic String sayHello(String name);}

HelloWSImpl.java實現HelloWS.java介面

@WebServicepublic class HelloWSImpl implements HelloWS{@Overridepublic String sayHello(String name) {System.out.println("Server says hello to " + name);return "Hello " + name;}}

ServerTest發布服務

/** * 發布Webservice * */public class ServerTest {public static void main(String[] args) {String address = "http://192.168.13.232:8989/ws_01/umgsai";Endpoint.publish(address, new HelloWSImpl());System.out.println("發布成功");}}

用戶端

①使用JDK內建的wsimport工具根據網路url來產生用戶端代碼

在cmd中進入到目標目錄,例子中是client項目中的src目錄,執行

wsimport -keep http://192.168.13.232:8989/ws_01/umgsai?wsdl

執行完後會在src中產生一下目錄和檔案

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4C/29/wKioL1Q3d-3jTWQaAAB-Oxac2vw074.jpg" title="QQ20141010140712.png" alt="wKioL1Q3d-3jTWQaAAB-Oxac2vw074.jpg" />

②使用JDK內建的wsimport工具根據wsdl檔案來產生用戶端代碼

在瀏覽器中開啟

http://192.168.13.232:8989/ws_01/umgsai?wsdl

然後將代碼儲存為umgsai.wsdl檔案。將檔案中的8989連接埠改成8080

然後在Eclipse中配置一個連接埠轉寄

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/2A/wKioL1Q3f2yA1C7BAADkcaISkZM713.jpg" title="QQ20141010143927.png" alt="wKioL1Q3f2yA1C7BAADkcaISkZM713.jpg" />

在cmd中進入到目標目錄,例子中是client項目中的src目錄,執行

wsimport -keep E:\umgsai\workspace\WS_Client\src\umgsai.wsdl

執行完後會在src中產生一下目錄和檔案

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4C/29/wKioL1Q3d-3jTWQaAAB-Oxac2vw074.jpg" title="QQ20141010140712.png" alt="wKioL1Q3d-3jTWQaAAB-Oxac2vw074.jpg" />


用戶端調用

然後在ClientTest.java中調用Server端的函數

public class ClientTest {public static void main(String[] args) {HelloWSImplService factory = new HelloWSImplService();HelloWSImpl helloWS = factory.getHelloWSImplPort();System.out.println(helloWS.getClass());String result = helloWS.sayHello("Tom");System.out.println(result);}}


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/28/wKiom1Q3gLPC0vJZAAH8imENF3M577.jpg" title="QQ20141010144541.png" alt="wKiom1Q3gLPC0vJZAAH8imENF3M577.jpg" />

本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1562143

WebService學習筆記-Hello world

聯繫我們

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