web service--基礎概念(1)

1. schema約束javascript

       一  幾個重要知識:  html

                1 .  namespace           至關於Schema文檔的id,它的值必須是惟一java

                2.   targetNamespace    屬性用來指定schema文檔的namespace值jquery

                3.   xmlns                         屬性   引入某個命名空間web

                4.   schemaLocation    屬性  指定引入的命名空間的 schema  文件的位置
      二 Schema 規範
               1.  xml文件中的全部標籤和屬性都須要有schema文件來定義(約束)
 
               2.  如何引入約束?  xmlns屬性來指定:它的值爲一個schema文件的namespace值
 
               3.  每一個schmema文件都必須有一個惟一標識,日常通常取名爲id,但在schema中以namespace(命名空間)來表達
                      也就是每一個Schema文件都有一個惟一的namespace值
             
               4.   schema文件的namespace值如何指定?
                     targetNamespace  屬性來指定:它的值是一個url    格式的文本 (路徑不必定真實存在,但必須惟一)
 
               5.    若是引入的schema約束不是w3c組織定義, 那麼在引入後還須要指定schema文件的位置
              
               6.   如何來指定schema文件的位置? schemaLocation 屬性來指定:它的值由兩部分組成:namcespace+path
 
               7.  若是引入了N個約束, 那反必須給n-1個取別名,  在使用到某個取了別名的schema文檔的標籤或屬性時,必須經過別名來引導
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.zhouwuji.cn"
        elementFormDefault="qualified">
    <!-- qualified 關聯約定全部的標籤 默認爲unqualified -->
    <element name="books">
        <complexType>
        <!-- 複合類型  unbounded 無限的 -->
            <sequence maxOccurs="unbounded">
                <element name="book">
                    <complexType>
                        <sequence maxOccurs="1">
                            <element name="bookname" type="string" />
                            <element name="author" type="string" />
                            <element name="price" type="string" />
                        </sequence>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>
</schema>
text.xsd
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.zhouwuji.cn"
       xmlns:ss="http://www.w3.org/2001/XMLSchema-instance"
       ss:schemaLocation="http://www.zhouwuji.cn test.xsd">
    <book>
        <bookname>javascript</bookname>
        <author>淘氣老師</author>
        <price>¥32.1</price>
    </book>
</books>
<!--
  schema規範中:
   1.全部的標籤和屬性都須要有schema文件來定義
   2.全部的schema文件都須要有一個id,但在這裏他叫namespace
   3.namespace的值由什麼來決定?
          有targetNamespace屬性來指定,必須制定schema文件的位置
   4.如何引用一個schema約束?
           屬性:用xmlns屬性
           屬性值:對應的schema文件的id
   5.若是引入的schema不是w3c組織定義,必須指定schema文件的位置
   6.shcema文件的位置有什麼屬性指定?
            屬性:schemaLocation
            屬性值:namespace   path  
   7.若是引入N個約束,須要給n-1個取別名
   
   
   
   
-->
book.xml

 2 關於 Web Service 的幾個問題ajax

                   1. 基於 Web 的服務:服務器端整出一些資源讓客戶端應用訪問(獲取數據)
                   2. 一個跨語言、跨平臺的規範(抽象)
                   3. 多個跨平臺、跨語言的應用間通訊整合的方案
                    例: (實際) 以各個網站顯示天氣預報功能爲例: 氣象中心的管理系統將收集的天氣信息並將數據暴露出來(經過 WebService Server),
                             而各大站點的應用就去調用它們獲得天氣信息並以不一樣 的樣式去展現(WebService Client). 網站提供了天氣預報的服務,
                             但其實它們什麼也沒有作,只是簡單了調 用了一下氣象中心服務器上的一段代碼而已。
                                   

                  四、 爲何要用 Web service?  spring

                                  web service 能解決: 跨平臺調用 、跨語言調用 、遠程調用apache

                  5. 何時使用 web Service?  
                            --     同一家公司的新舊應用之間
                            --    不一樣公司的應用之間 分析業務需求:天貓網與中通物流系統如何交互?
                            --     一些提供數據的內容聚合應用:天氣預報、股票行情
                    

3. Web Service 中的幾個重要術語跨域

                      1. WSDL:web service definition language
                            a   直譯 : WebService 定義語言
                            b.  對應一種類型的文件.wsdl
                            c.  定義了 web service 的服務器端與客戶端應用交互傳遞請求和響應數據的格式 和方式
                            d.  一個 web service 對應一個惟一的 wsdl 文檔
                   .  2. SOAP:simple object access protocal
                            a    直譯: 簡單對象訪問協議
                            b.   是一種簡單的、基於 HTTP 和XML的協議, 用於在 WEB 上交換結構化的數據
                            c.   soap 消息:請求消息和響應消息
                            d.   http+xml 片段
                      3. SEI:WebService EndPoint Interface
                             a   直譯: web service 的終端接口,
                             b    就是 WebService 服務器端用來處理請求的接口
                     4. CXF:Celtix + XFire 
                              a  一個 apache 的用於開發 webservice 服務器端和客戶端的框架
  4. 開發 webservice
                    1. 概述
                                  a   開發手段:
                                          – 使用 JDK 開發(1.6 及以上版本)
                                          – 使用 CXF 框架開發(工做中)
                                  b   組成:
                                           – 服務器端
                                            – 客戶端
                      2. 使用 JDK 開發 WebService 1)     查看代碼   
                                  a    開發服務器端 • Web Service 編碼:
                                        – @WebService( SEI 和 SEI 的實現類)
                                        – @WebMethod(SEI 中的全部方法)
                                   b   發佈 Web Service:
                                         – Endpoint(終端, 發佈 webservice)
                        3. 開發客戶端
                                    a  使用 eclipse 提供的 web service 瀏覽器訪問
                                         – 查看對應的 wsdl 文檔:…..?wsdl (通常瀏覽器)
                                         – 請求 webService 並查看請求和響應消息(webservice 瀏覽 器)
                                   b   建立客戶端應用編碼方式訪問
                                         – 藉助 jdk 的 wsimort.exe 工具生成客戶端代碼:
                                               wsimport -keep url //url 爲 wsdl 文件的路徑
                                        – 藉助生成的代碼編寫請求代碼
                        4 . 調用免費的 web service(天氣預報)
                            a . 客戶端編碼方式訪問
                                         – 藉助命令工具自動生成客戶端代碼
                                         – 藉助生成的代碼編寫請求代碼
                                         --  http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
                                   b   注意: 直接生成客戶端代碼會拋異常, 沒法生成客戶端代碼,a
                                             解決辦法:
                                                  --. 將對應的 wsdl 文檔保存到本地
                                                  --. 修改 wsdl 文檔的部份內容: 將 替換成 備註: 這個是 Java 調用 net 的 webservice 都有的問題
                    5.分析 WebService 的 WSDL 文檔結構
                                    a   文檔結構
                                       <definitions> 
                                                       <types>
                                                             <schema>
                                                                      <element> 
                                                        </types>
                                                       <message>
 
                                                                   <part>
                                                       <message>                 
                                                       <portType>
                                                                 <operation>
                                                                        <input>
                                                                        <output>
                                                         </portType>
                                                         <binding>
                                                                <operation>
                                                                          <input>
                                                                          <output>
                                                          </binding>
                                                          <service>
                                                                       <port>
                                                                                <address>
                                                           </service>
                                                   </definitions>
                                            b.文檔結構圖
                   
                    
                                             重點:
                                                 types - 數據類型(標籤)定義的容器,裏面使用 schema 定義了一 些標籤結構供 message 引用
                                                   message - 通訊消息的數據結構的抽象類型化定義。引用 types 中定義的標籤
                                                   operation - 對服務中所支持的操做的抽象描述,一個 operation 描述了一個訪問入口的請求消息與響應消息對。
                                                 portType - 對於某個訪問入口點類型所支持的操做的抽象集合, 這些操做能夠由一個或多個服務訪問點來支持。
                                                 binding - 特定端口類型的具體協議和數據格式規範的綁定。
                                                 service- 相關服務訪問點的集合
                                                 port - 定義爲協議/數據格式綁定與具體 Web 訪問地址組合的單 個服務訪問點。                                 
                          6 測試 CXF 支持的數據類型
                                         a. 基本類型     –    int,float,boolean 等
                                         b. 引用類型     – String      – 集合:數 1 組,List, Set, Map       – 自定義類型 Student
                          7   一次 Web service 請求的流程
                                   a.  一次 web service 請求的本質:
                                         1) 瀏覽器向服務器端發送了一個 soap 消息(http 請求+xml 片段)  
                                         2) 服務器端處理完請求後, 向客戶端返回一個 soap 消息 那麼它的流程是怎樣的呢?
 
                                             

                             8  CXF 框架的深刻使用  查看代碼瀏覽器

                                     a  .CXF 的攔截器
                                             • 爲何設計攔截器?
                                                     1. 爲了在 webservice 請求過程當中,能動態操做請求和響應數 據, CXF 設計了攔截器.
                                              • 攔截器分類:
                                                     1. 按所處的位置分:服務器端攔截器,客戶端攔截器
                                                     2. 按消息的方向分:入攔截器,出攔截器
                                                     3. 按定義者分:系統攔截器,自定義攔截器
                                                 攔截器
                                                      API Interceptor(攔截器接口)
                                                                     AbstractPhaseInterceptor(自定義攔截器今後繼承)
                                                                     LoggingInInterceptor(系統日誌入攔截器類)
                                                                     LoggingOutInterceptor(系統日誌出攔截器類)
                                      b  編碼實現攔截器
                                                      • 使用日誌攔截器,實現日誌記錄  
                                                                   – LoggingInInterceptor
                                                                   – LoggingOutInterceptor
                                                      • 使用自定義攔截器,實現用戶名與密碼的檢驗
                                                                 – 服務器端的 in 攔截器
                                                                 – 客戶端的 out 攔截器
                                                                 – xfzhang/123456
                                      c、. 用 CXF 編寫基於 spring 的 web service 2.1).
                                              編碼實現
                                                       1. Server 端
                                                                        – 建立 spring 的配置文件 beans.xml,在其中配置 SEI
                                                                        – 在 web.xml 中,配置上 CXF 的一些核心組件
                                                       2. Client 端 – 生成客戶端代碼 – 建立客戶端的 spring 配置文件 beans-client.xml,並配置 – 編寫測試類請求 web service
                                           . 添加自定義攔截器
                                                      1. Server 端 – 在 beans.xml 中,在 endpoint 中配置上入攔截器
                                                       2. Client 端 – 經過 Client 對象設置出攔截器
                                9   Ajax 調用 webService  查看代碼
                                         a  原生的js 使用ajax 請求                                          
function reqWebService() {
        var name = document.getElementById("name").value;
        var date = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><authHeader><userId>wl</userId><password>1985310</password></authHeader>
</soap:Header><soap:Body><ns2:getOrderById xmlns:ns2="http://ws.zhouwuji.com/"><arg0>' + name + '</arg0></ns2:getOrderById></soap:Body></soap:Envelope>'; var xmlhttp = getRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var result = xmlhttp.responseXML; var returnEle1 = result.getElementsByTagName("id")[0]; var returnEle2 = result.getElementsByTagName("name")[0]; var returnEle3 = result.getElementsByTagName("price")[0]; var vale1 = returnEle1.firstChild.data; var vale2 = returnEle2.firstChild.data; var vale3 = returnEle3.firstChild.data; alert(vale1+vale2+vale3); } }; //localhost 不能用ip請求 對於js講跨域請求 協議/ip/端口 保持一致 不然跨域 xmlhttp.open("POST","http://localhost:8080/webservicesCXF-spring/orderws"); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(date); } function getRequest() { var xmlhttp = null; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; }
  
                                                    跨域請求問題: 
                                                             1. 什麼是跨域請求? 協議 ip  端口  同樣不跨域
                                                                         1. sina.com--=->baidu.com/xxx.jsp
                                                                          2. localhost----192.168.42.165
                                                              2. 解決 ajax 跨域請求 webservice 的問題?
                                                                    在客戶端應用中使用 java 編碼去請求 webservice, 在頁面中去請求 本身的後臺
                                          b  jquery請求  

                                         

$("#btn1").click(function() {
        var name = document.getElementById("name").value;
        var date = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><authHeader><userId>wl</userId><password>1985310</password>
</authHeader> </soap:Header><soap:Body><ns2:getOrderById xmlns:ns2="http://ws.zhouwuji.com/"><arg0>' + name + '</arg0></ns2:getOrderById></soap:Body></soap:Envelope>'; alert(date); $.ajax({ type : "POST", url : "http://localhost:8080/webservicesCXF-spring/orderws", data : date, success : function(msg) { alert("------"); var $Result = $(msg); var value = $Result.find( "return").text(); alert(value); }, error : function(msg) { //alert("-----"+msg); }, dataType : "xml" }); });

 

 

 

                                          c 解決跨域到servlet 後臺java代碼請求 
                                        
String path = "http://localhost:8080/webservicesCXF-spring/orderws";
         String data = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Header><authHeader><userId>wl</userId><password>1985310</password>
</authHeader></soap:Header><soap:Body><ns2:getOrderById xmlns:ns2='http://ws.zhouwuji.com/'><arg0>" + name + "</arg0></ns2:getOrderById></soap:Body></soap:Envelope>"; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); OutputStream os = connection.getOutputStream(); os.write(data.getBytes("utf-8")); os.flush(); int code = connection.getResponseCode(); if(code==200){ InputStream is = connection.getInputStream(); response.setContentType("text/xml;charset=utf-8"); ServletOutputStream outputStream = response.getOutputStream(); int len=0; byte[] buff = new byte[1024]; while((len = is.read(buff))>0){ outputStream.write(buff,0,len); } outputStream.flush(); outputStream.close(); } }
 
 5. 經過註解修改 wsdl 文檔  JDK 中的相關注解
                       
                           a  @WebService
                       

                                  

                       b   @WebMethod

 
                              

                        c  @WebResult

                             

                            d   @WebParam 

                             

                           e  @XmlElement

                               

                      注: 說明 即便是沒有修改源代碼,只修改了註解,客戶端的代碼也必需要重 新生成, 不然調用將會失敗。

相關文章
相關標籤/搜索