JAVA web service --- overview

来源:互联网 发布:数据库管理系统课题 编辑:程序博客网 时间:2024/04/30 22:15

JAVA web service --- overview

<script type="text/javascript"> document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData(&quot;text&quot;); if (text &amp;&amp; text.length &gt; 300) { text = text + &quot;\r\n\n本文来自CSDN博客,转载请标明出处:&quot; + location.href; clipboardData.setData(&quot;text&quot;, text); } }, 100); } } </script><script type="text/javascript"> function StorePage() { d = document; t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : ''); void (keyit = window.open('http://www.365key.com/storeit.aspx?t=' + escape(d.title) + '&amp;u=' + escape(d.location.href) + '&amp;c=' + escape(t), 'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }</script>

    Web service的出现是为了解决不同平台上的软件应用系统间的相互通信,比如发布在Unix上的用JAVA开发的付费系统想调用.Net开发的发布在NT 上的库存系统的某个功能。早些时,这种需求叫做EAI(企业软件集成)。后来,人们就想,既然这种需求会在以后出现,为什么不在开发的时候就考虑进去呢,比如在开发库存系统的时候,除了用户可以通过GUI查询到库存之外,还给别的系统提供了一个程序接口。这种设计被叫做SOA(面向服务的架构).【除了 Web service能够完成这种需求,另外经常用到的就是Messaging,消息中间件】

    说到Web service,必须说说WSDL和SOAP。WSDL描述了提供 service的家伙都能干些什么,SOAP是来往通信的内容应该是什么样的格式。这两个东西能够大红大紫,是因为它们站在了巨人XML的肩膀上,尤其是WSDL。

    WSDL的全称是 web service description language,是XML格式,根节点是definitions,它有五个子节点,types,message,portType,binding和 service。它们之间的关系是types被message用,message被portType和binding用,portType和 binding被service用。
    
    拿到一个WSDL文件后,最好是从service开始看。
    service:描述了这个service的名称,这个service的地址,还有它的port和binding。
    e.g.
    <wsdl:service name="WSCXFProviderService">
        <wsdl:port name="WSCXFProviderPort"
            binding="tns:WSCXFProviderServiceSoapBinding">
            <soap:address
                location="http://localhost:8080/WSCXFProviderPort" />
        </wsdl:port>
    </wsdl:service>
    
    binding:描述调用webservice时候的通信协议和SOAP包形成的一些细节。【transport,style,use】
    一个binding里可以有多个operation,可以把operation理解为这个web service提供的一个功能(函数).
    e.g.
    <wsdl:binding name="WSCXFProviderServiceSoapBinding"
        type="tns:WSCXFProvider">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="testWS">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="testWS">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="testWSResponse">
                <soap:body use="literal" />
            </wsdl:output>
            <wsdl:fault name="IllegalArgumentException">
                <soap:fault name="IllegalArgumentException"
                    use="literal" />
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
 
  portType:描述了这个service能够提供些什么样的功能,和这些功能交互时候的消息格式。
  同样,一个portType里可以有多个operation,可以把portType理解为JAVA中的接口(Interface),operation就是这些接口定义的函数。
  e.g.
    <wsdl:portType name="WSCXFProvider">
        <wsdl:operation name="testWS">
            <wsdl:input name="testWS" message="tns:testWS"></wsdl:input>
            <wsdl:output name="testWSResponse"
                message="tns:testWSResponse">
            </wsdl:output>
            <wsdl:fault name="IllegalArgumentException"
                message="tns:IllegalArgumentException">
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:portType>
 
    message:定义了调用某个功能时候的request,response,falt的格式。
    e.g.
    <wsdl:message name="testWS">
        <wsdl:part name="parameters" element="tns:testWS"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="IllegalArgumentException">
        <wsdl:part name="IllegalArgumentException"
            element="tns:IllegalArgumentException">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="testWSResponse">
        <wsdl:part name="parameters" element="tns:testWSResponse">
        </wsdl:part>
    </wsdl:message>
 
    types:定义了message里request,response,falt的具体type。
    【不在message里直接定义types,是types可以重用】
    e.g.
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://test.cxf.bt.com/"
            attributeFormDefault="unqualified" elementFormDefault="unqualified"
            targetNamespace="http://test.cxf.bt.com/">
            <xs:element name="testWS" type="tns:testWS" />
            <xs:element name="testWSResponse" type="tns:testWSResponse" />
            <xs:complexType name="testWS">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0"
                        type="xs:string" />
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="testWSResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return"
                        type="xs:string" />
                </xs:sequence>
            </xs:complexType>
            <xs:element name="IllegalArgumentException"
                type="tns:IllegalArgumentException" />
            <xs:complexType name="IllegalArgumentException">
                <xs:sequence />
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    
    SOAP:simple object access protocal,把它叫做protocal,经常让人误解为通信协议,其实它应该是一种格式协议。

    Envelope 元素是 SOAP 消息的根元素。
    可选的 SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。
    必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。
    Body里面是消息的消息体(payload)或者出错时候的一个SOAP Fault 元素,如果已提供了 Fault 元素,则它必须是 Body 元素的子元素。在一条 SOAP 消息中,Fault 元素只能出现一次。
    
    e.g.根据上面WSDL的SOAP request:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test.cxf.bt.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <test:testWS>
             <arg0>message passed in</arg0>
          </test:testWS>
       </soapenv:Body>
    </soapenv:Envelope>
    返回的一个SOAP response:
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <ns2:testWSResponse xmlns:ns2="http://test.cxf.bt.com/">
             <return>Returned: message passed in</return>
          </ns2:testWSResponse>
       </soap:Body>
    </soap:Envelope>
    出错时候包含fault的response:
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <ns1:Fault xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
             <faultcode>ns1:Server</faultcode>
             <faultstring>illegal argument.</faultstring>
             <detail>
                <ns1:IllegalArgumentException xmlns:ns1="http://test.cxf.bt.com/"/>
             </detail>
          </ns1:Fault>
       </soap:Body>
    </soap:Envelope>

转自:http://494078416.iteye.com/blog/684807
原创粉丝点击