標籤:images pattern 資訊 index ini html apach 視窗 操作
Web service是一個平台獨立的,低耦合的,自包含的、基於可編程的web的應用程式,可使用開放的XML(標準通用標記語言 (SGML)下的一個子集)標準來描述、發布、發現、協調和配置這些應用程式,用於開發分布式的互操作的應用程式。
Web Service技術, 能使得運行在不同機器上的不同應用無須藉助附加的、專門的第三方軟體或硬體, 就可相互交換資料或整合。依據Web Service規範實施的應用之間, 無論它們所使用的語言、 平台或內部協議是什麼, 都可以相互交換資料。Web Service是自描述、 自包含的可用網路模組, 可以執行具體的業務功能。Web Service也很容易部署, 因為它們基於一些常規的產業標準以及已有的一些技術,諸如標準通用標記語言 (SGML)下的子集XML、HTTP。Web Service減少了應用介面的花費。Web Service為整個企業甚至多個組織之間的商務程序的整合提供了一個通用機制。
WebService就是實現多個系統之間的遠程調用技術。WebService可以實現跨語言的調用。
- WebService,顧名思義就是基於Web的服務。它使用Web(HTTP\ XML)方式,接收和響應外部系統的某種請求。從而實現遠程調用.
- 我們可以調用互連網上查詢天氣資訊Web服務,然後將它嵌入到我們的程式(C/S或B/S程式)當中來,當使用者從我們的網點看到天氣資訊時,他會認為我們為他提供了很多的資訊服務,但其實我們什麼也沒有做,只是簡單調用了一下伺服器上的一段代碼而已。
- 學習WebService可以將你的服務(一段代碼)發布到互連網上讓別人去調用,也可以調用別人機器上發布的WebService,就像使用自己的代碼一樣
一.java代碼調用webservice實現國內手機號碼歸屬地查詢服務
先開啟http://www.webxml.com.cn/zh_cn/index.aspx 網站:找到國內手機號碼歸屬地查詢服務
進入http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx 網站
然後進入http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 網站
然後在案頭開啟dos命令視窗
wsimport -s . -p com.baoyuan.ws http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
輸入上面的代碼 -p 後面的表示包名 可以自己定義
得到的源碼和calss檔案,可以將Class檔案刪掉,留下源碼
然後建立工程,將包添加進去
編寫測試代碼,在http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 中找到wsdal:service 的name值為服務類
在http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 中找到wsdl:port 的那麼值為代理類,可以通過服務類的getter方法獲得
wsdl:operation的name值為方法,通過代理類調用
package com.baoyuan.test;import com.baoyuan.ws.MobileCodeWS;import com.baoyuan.ws.MobileCodeWSSoap;public class TestWebService { public static void main(String[] args) { //執行個體化服務 MobileCodeWS ws = new MobileCodeWS(); //擷取代理對象 MobileCodeWSSoap soap = ws.getMobileCodeWSSoap(); //通過代理對象調用方法 String info = soap.getMobileCodeInfo("18662584581", ""); System.out.println(info); }}
二.直接通過jdk的java代碼發布webservice服務:
package com.baoyuan.server;import javax.jws.WebService;import javax.xml.ws.Endpoint;/** * 普通一個類實現webService發布服務 * * @author admin * */
//只需要在類上面加上webService的註解
@WebServicepublic class HelloService { public String sayHell(String name){ return name+"來了"; } public static void main(String[] args) { //通過jdk提供的Endpoint.publish方法將訪問地址:ip+連接埠+服務名稱 以及 服務物件傳入,發布服務 Endpoint.publish("http://localhost:8080/hello", new HelloService()); System.out.println("服務啟動成功"); }}
通過CXF架構與spring架構整合發布webservice
建立web工程
web.xml配置cxf
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>cxf_server</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置cxf 的servlet--> <servlet> <servlet-name>cxfServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <!-- 兩種方式classpath WEB-INF --> <!-- 將applicationContext.xml放在src目錄下 <param-value>classpath:applicationContext.xml</param-value> --> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cxfServlet</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping></web-app>
spring的設定檔applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 為什麼使用applicationContext.xml spring和cxf整合 通過一個檔案進行配置 --> <!-- 方式一: address訪問服務地址 http://ip:連接埠/服務名稱 implementorClass實作類別的全路徑 --> <jaxws:endpoint address="/moble" implementorClass="com.baoyuan.ws.service.impl.MobleAddressServiceImpl"> </jaxws:endpoint> <!-- 註冊一個實作類別 --> <!-- <bean id="helloService" class="cn.itcast.cxf.HelloServiceImpl"></bean> --> <!-- 方式二: id:如果action或service注入的時候 --> <!-- <jaxws:server address="/hello" > <jaxws:serviceBean> <ref bean="helloService"/> </jaxws:serviceBean> </jaxws:server> --></beans>
建立業務介面,類名上要加webService註解
package com.baoyuan.ws.service;import javax.jws.WebParam;import javax.jws.WebService;/** * 類比根據手機號碼查詢歸屬地 * @author Administrator * *///只需要在介面上加上webService註解@WebServicepublic interface MobleAddressService { //類比根據手機號碼查詢歸屬地 WebParam(name="mobleNo")註解表示下載後的源碼參數的名字還是mobleNo不會改變 public String getMobleAddress(@WebParam(name="mobleNo")String mobleNo);}
建立實作類別
package com.baoyuan.ws.service.impl;import com.baoyuan.ws.service.MobleAddressService;/** * 類比根據手機號碼查詢歸屬地 * @author Administrator * */public class MobleAddressServiceImpl implements MobleAddressService{ //類比根據手機號碼查詢歸屬地 @Override public String getMobleAddress(String mobleNo) { String result=mobleNo+" 歸屬地:長沙"; return result; }}
然後訪問url: http://localhost:8080/cxf_server/cxf/moble?wsdl 注釋:cxf_server為項目名稱,/cxf為在web.xml中配置的訪問服務的路徑,/moble為在spring設定檔中配置的address
<wsdl:definitions xmlns:ns1="http://service.ws.baoyuan.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.ws.baoyuan.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MobleAddressServiceImplService" targetNamespace="http://impl.service.ws.baoyuan.com/"><wsdl:import location="http://localhost:8080/cxf_server/cxf/moble?wsdl=MobleAddressService.wsdl" namespace="http://service.ws.baoyuan.com/"></wsdl:import><wsdl:binding name="MobleAddressServiceImplServiceSoapBinding" type="ns1:MobleAddressService"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="getMobleAddress"><soap:operation soapAction="" style="document"/><wsdl:input name="getMobleAddress"><soap:body use="literal"/></wsdl:input><wsdl:output name="getMobleAddressResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="MobleAddressServiceImplService"><wsdl:port binding="tns:MobleAddressServiceImplServiceSoapBinding" name="MobleAddressServiceImplPort"><soap:address location="http://localhost:8080/cxf_server/cxf/moble"/></wsdl:port></wsdl:service></wsdl:definitions>
編寫用戶端測試代碼,下載用戶端代碼
wsimport -s . -p com.baoyuan.ws http://localhost:8080/cxf_server/cxf/moble?wsdl
建立工程,將下載的用戶端代碼複製複製過來,注意:代碼可以在下載時用 -p指定包名,不允許下載完成後修改包名
編寫測試代碼,注意:測試時:服務端必須開啟
package com.baoyuan.ws.test;import com.baoyuan.ws.MobleAddressService;import com.baoyuan.ws.MobleAddressServiceImplService;public class TestCXF { public static void main(String[] args) { //執行個體化服務 MobleAddressServiceImplService service = new MobleAddressServiceImplService(); //建立代理對象 MobleAddressService serviceImplPort = service.getMobleAddressServiceImplPort(); //調用方法 String mobleAddress = serviceImplPort.getMobleAddress("13333532525"); System.out.println(mobleAddress); }}
webservice的簡單使用,cxf架構的的使用