一直在接觸WebService 推送這塊,今天記下來,以便後續查看
1,什麼是WebService?
1),簡單理解基於web的遠程調用,主要是爲了使原來各孤立的站點之間的信息能夠相互通信、共享而提出的一種接口。
2),Web Service也叫XML Web Service WebService是一種可以接收從Internet或者Intranet上的其它系統中傳遞過來的請求,輕量級的獨立的通訊技術。是:通過SOAP在Web上提供的軟件服務,使用WSDL文件進行說明,並通過UDDI進行註冊。
2,WebService優點以及什麼情況下使用
1),Web Service所使用的是Internet上統一、開放的標準,如HTTP、XML、SOAP(簡單對象訪問協議)、WSDL等,所以Web Service可以在任何支持這些標準的環境(Windows,Linux)中使用。
注:SOAP協議(Simple Object Access Protocal,簡單對象訪問協議),它是一個用於分散和分佈式環境下網絡信息交換的基於XML的通訊協議。在此協議下,軟件組件或應用程序能夠通過標準的HTTP協議進行通訊。它的設計目標就是簡單性和擴展性,
import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style=Style.RPC) public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed(); }
再編寫 Web Service 實現:
import java.text.DateFormat; import java.util.Date; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(endpointInterface = "myweb.service.TimeServer") public class TimeServerImpl implements TimeServer { /** * 返回從1970年1月1日0點0時0分起的毫秒數 */ public long getTimeAsElapsed() { return new Date().getTime(); } /** * 返回如“2009-12-21”格式的日期 */ public String getTimeAsString() { Date date = new Date(); DateFormat df = DateFormat.getDateInstance(); return df.format(date); } public static void main(String[] args){ // 第一個參數是發佈的URL // 第二個參數是SIB(Service Implementation Bean)實現 Endpoint.publish("http://localhost:8083/myweb11", new TimeServerImpl()); //192.168.1.225 System.out.println("---"); } }
最後啓動 Web Service:
import javax.xml.ws.Endpoint; public class TimeServerPublisher { public static void main(String[] args){ // 第一個參數是發佈的URL // 第二個參數是SIB(Service Implementation Bean)實現 Endpoint.publish("http://127.0.0.1:8080/mywebsss", new TimeServerImpl()); System.out.println("---"); } }
網址打開
2,WebService客戶端
使用wsimport -s E:\workspace\Web\src -p ht_service_test -keep http://127.0.0.1:8080/mywebsss?wsdl 生成客戶端代碼, 接下來直接調用服務端接口即可
-s 生成所在路徑
-p 包名
-keep wsdl地址 <也可將wsdl保存到本地>
記錄點點滴滴,雖然微不足道。