標籤:string 右鍵 cep package 結果 delegate ges 技術分享 返回
一、伺服器端
1、建立web項目,項目名稱為 WS_Server
2、建立包,並在該包下建立TestService類:
package com.test.webservice;public class TestService { public static final String TITLE = "此訊息由WebService伺服器端返回: "; public String test(String msg){ return TITLE + msg; }}
3、將TestService.java類產生為伺服器端:
(1)類名-右鍵-Web Service-Web Service
(2)點擊 Next
(3)點擊Next,選擇想要變成伺服器端的類,選中完畢後,直接點擊Finish按鈕。
(4)可在包下發現多了一個TestServiceDelegate.java類;WEB-INF下多了一個sun-jaxws.xml檔案。
(5)加入jar包,並允許Tomcat。 完成之後,在瀏覽器地址欄中輸入: http://localhost:8080/WS_Server/TestServicePort?wsdl ,結果如下:
二、用戶端
(1)建立Web項目,項目名稱為:WS_Client
(2)建立Web Service Client,方法如伺服器端,可以使用如下快捷按鈕
(3)輸入伺服器端的WSDL地址: ,點擊Next,就會產生用戶端的檔案。
(4)建立TestClient.java
package com.test.webservice;public class TestClient { public static void main(String[] args) { TestServiceDelegate test = new TestServiceService().getTestServicePort(); String result = test.test("--------調用伺服器端的test方法----------"); // 調用伺服器端的test方法 System.out.println("------&&&&&見證結果&&&&&&------"); System.out.println(result); }}
(5)將項目加入到Tomcat,啟動伺服器,並執行用戶端的TestClient,執行結果如下:
使用JAX-RS進行Web Service介面開發