一、---------------------------------介紹--------------------------------------------------java
(1)遠程調用:一個系統遠程調用另外一個系統的服務,已獲取遠程系統的業務數據。web
(2)爲何使用:基於安全性的考慮,通常企業不會開放本身的數據庫,只能使用遠程調用技術。數據庫
2----------------------- Webservice是如何實現遠程調用?三要素在webservice的做用?-----------------------------------編程
(1)Webservice原理:webservice是一種使用http傳輸的SOAP協議的數據的遠程調用技術。安全
(2) WSDL做用:webservice服務端的使用說明書。(Web Service Description Language)ide
(3)SOAP做用:規範XML標籤。工具
(4) UDDI:提供webservice服務端的搜索和註冊功能。性能
注意事項:開發工具
3----------------------------Webservice入門程序--------------------------------------------------測試
(1) 服務端
第一步:建立SEI(Service Endpoint Interface)接口,本質就是Java接口
public interface WeatherInterface {
public String queryWeather(String Name);
}
l 第二步:建立SEI實現類
@WebService
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String Name) {
System.out.println("from client..."+Name);
String weather = "晴";
return weather;
}
}
l 第三步:發佈服務
public class WeatherServer {
public static void main(String[] args) {
//用Endpoint類的publish方法發佈服務
//端口號建議不要過短,容易被系統佔用;不要太長,太長不支持
Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
}
}
l第四步:測試服務是否發佈成功,經過閱讀服務端的使用說明書,肯定關鍵元素存在(類/方法/參數等),表示服務發佈成功
注意:
l第一步:在實現類上加入以下註解
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
l第二步:引入第三方插件的jar包(jaxws-ri-2.2.8)
第三步:從新啓動服務端
(2)客戶端(好幾種方法實現)
開發步驟:
第一步:wsimport命令生成客戶端代碼。
wsimport -s . http://127.0.0.1:12345/weather?wsdl --------------------------若是你的開發工具是IDEA能夠直接使用工具,裏面有,能夠百度一下。
第二步:建立服務視圖,服務視圖名的類從service標籤的name屬性獲取
第三步:經過服務視圖獲取對應服務實現類的實例,服務實現類的名稱從portType的name屬性獲取
第四步:調用查詢方法,方法名從operation下的name屬性獲取
public class WeatherClient {
public static void main(String[] args) {
//建立服務視圖
WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
//獲取服務實現類的實例
String weather = weatherInterfaceImpl.queryWeather("北京");
System.out.println(weather);
}
}
5-------------------------- Webservice優缺點--------------------------
優勢:
缺點:
6----------------------------使用------------------------------------
適用場景:
不適用場景: