web service--基础概念(1)

1. schema约束

       一  几个重要知识:  

                1 .  namespace           相当于Schema文档的id,它的值必须是唯一

                2.   targetNamespace    属性用来指定schema文档的namespace值

                3.   xmlns                         属性   引入某个命名空间

                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 的几个问题

                   1. 基于 Web 的服务:服务器端整出一些资源让客户端应用访问(获取数据)
                   2. 一个跨语言、跨平台的规范(抽象)
                   3. 多个跨平台、跨语言的应用间通信整合的方案
                    例: (实际) 以各个网站显示天气预报功能为例: 气象中心的管理系统将收集的天气信息并将数据暴露出来(通过 WebService Server),
                             而各大站点的应用就去调用它们得到天气信息并以不同 的样式去展示(WebService Client). 网站提供了天气预报的服务,
                             但其实它们什么也没有做,只是简单了调 用了一下气象中心服务器上的一段代码而已。
                                   

                  4、 为什么要用 Web service?  

                                  web service 能解决: 跨平台调用 、跨语言调用 、远程调用

                  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

                               

                      注: 说明 即使是没有修改源代码,只修改了注解,客户端的代码也必须要重 新生成, 否则调用将会失败。

 
 
原文地址:https://www.cnblogs.com/ou-pc/p/8324894.html