伺服器端
編碼:
a. 建立一個基於jdk6以上版本的java工程
b. 定義SEI web service Endpoint interface(web service終端介面)
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
c. 定義SEI的實作類別:
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
System.out.println("sayHello "+name);
return "hello "+name;
}
}
發布:
public class Server {
public static void main(String[] args) {
//用戶端發送web service請求的url
String address="http://127.0.0.1/tg_ws/helloWS";
//處理請求的SEI對象
HelloWS helloWS = new HelloWSImpl();
//發布web service
Endpoint.publish(address, helloWS);
System.out.println("發布web service成功。");
}
}
(註:我的要建一個ServerDelegate 在Server基礎上點擊 開啟new=》other=》輸入 web service 點擊=》選擇 create web service from java class=》開啟Brouwse 輸入你的測試類別 最後勾上 Generate WSDL in project 會產生一個 * Delegate.java)
②. 用戶端
1. eclipse Web Service瀏覽器
a. 查看Web Service所對應的WSDL文檔:...?wsdl
b. 使用eclipse訪問
請求體:SOAP Request Envelope
<soapenv:Envelope>
<soapenv:Body>
<q0:sayHello>
<arg0>tt</arg0>
</q0:sayHello>
</soapenv:Body>
</soapenv:Envelope>
響應體:SOAP Response Envelope
<S:Envelope>
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="http://ws.java.atguigu.net/">
<return>hello tt</return>
</ns2:sayHelloResponse>
</S:Body>
</S:Envelope>
2. 編碼實現
a. 建立用戶端java應用
b. 在應用的src下執行cxf的命令產生用戶端代碼:
wsimport -keep http://127.0.0.1/tg_ws/helloWS?wsdl
c. 編寫用戶端調用的測試代碼,執行:
public class Client {
public static void main(String[] args) {
//建立SEI的工廠對象
HelloWSImplService factory = new HelloWSImplService();
//得到一個SEI實作類別對象
HelloWSImpl helloWS = factory.getHelloWSImplPort();
//調用SEI的方法,此時才去發送web Service請求,並得到返回結果
String result = helloWS.sayHello("Tom");
System.out.println(result);
}
}
③.請求過程記錄:使用eclipse的tcp/ip工具進行請求的監控