JAVA web service --- overview

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用,portType被binding用,binding被service用。
拿到一个WSDL文件后,最好是从service开始看。
service:描述了这个service的名称,这个service的地址,还有用到的binding('bingding=')。
  1. e.g.
  2. <wsdl:service name="WSCXFProviderService">
  3. <wsdl:port name="WSCXFProviderPort"
  4. binding="tns:WSCXFProviderServiceSoapBinding">
  5. <soap:address
  6. location="http://localhost:8080/WSCXFProviderPort" />
  7. </wsdl:port>
  8. </wsdl:service>
    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】
bingding通过 'type='关联到portType,一个binding里可以有多个operation,可以把operation理解为这个web service提供的一个功能,包含了style和use的定义(函数).
  1. e.g.
  2. <wsdl:binding name="WSCXFProviderServiceSoapBinding"
  3. type="tns:WSCXFProvider">
  4. <soap:binding style="document"
  5. transport="http://schemas.xmlsoap.org/soap/http" />
  6. <wsdl:operation name="testWS">
  7. <soap:operation soapAction="" style="document" />
  8. <wsdl:input name="testWS">
  9. <soap:body use="literal" />
  10. </wsdl:input>
  11. <wsdl:output name="testWSResponse">
  12. <soap:body use="literal" />
  13. </wsdl:output>
  14. <wsdl:fault name="IllegalArgumentException">
  15. <soap:fault name="IllegalArgumentException"
  16. use="literal" />
  17. </wsdl:fault>
  18. </wsdl:operation>
  19. </wsdl:binding>
    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>
Style可以为document和rpc:
RPC:传输的request是对要调用的方法的描述,方法名称,参数...
Document:传输的request是符合在type中对参数的定义的一个'文档',不需要有方法的描述.
use可以为literal和encding:
encoding:传输的request中的参数是soap规定的类型,每个element带有'xsd'指定的类型,如 <a xsi:type="xsd:int">1</a>...
Document:传输的requestelement中element没有类型说明,接受端根据WSDL中定义的type进行检查.
portType:描述了这个service能够提供些什么样的功能,和这些功能交互时候的消息格式。
同样,一个portType里可以有多个operation,可以把portType理解为JAVA中的接口(Interface),operation就是这些接口定义的函数。
  1. e.g.
  2. <wsdl:portType name="WSCXFProvider">
  3. <wsdl:operation name="testWS">
  4. <wsdl:input name="testWS" message="tns:testWS"></wsdl:input>
  5. <wsdl:output name="testWSResponse"
  6. message="tns:testWSResponse">
  7. </wsdl:output>
  8. <wsdl:fault name="IllegalArgumentException"
  9. message="tns:IllegalArgumentException">
  10. </wsdl:fault>
  11. </wsdl:operation>
  12. </wsdl:portType>
  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的格式。
  1. e.g.
  2. <wsdl:message name="testWS">
  3. <wsdl:part name="parameters" element="tns:testWS"></wsdl:part>
  4. </wsdl:message>
  5. <wsdl:message name="IllegalArgumentException">
  6. <wsdl:part name="IllegalArgumentException"
  7. element="tns:IllegalArgumentException">
  8. </wsdl:part>
  9. </wsdl:message>
  10. <wsdl:message name="testWSResponse">
  11. <wsdl:part name="parameters" element="tns:testWSResponse">
  12. </wsdl:part>
  13. </wsdl:message>
    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可以重用】
  1. e.g.
  2. <wsdl:types>
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:tns="http://test.cxf.bt.com/"
  5. attributeFormDefault="unqualified" elementFormDefault="unqualified"
  6. targetNamespace="http://test.cxf.bt.com/">
  7. <xs:element name="testWS" type="tns:testWS" />
  8. <xs:element name="testWSResponse" type="tns:testWSResponse" />
  9. <xs:complexType name="testWS">
  10. <xs:sequence>
  11. <xs:element minOccurs="0" name="arg0"
  12. type="xs:string" />
  13. </xs:sequence>
  14. </xs:complexType>
  15. <xs:complexType name="testWSResponse">
  16. <xs:sequence>
  17. <xs:element minOccurs="0" name="return"
  18. type="xs:string" />
  19. </xs:sequence>
  20. </xs:complexType>
  21. <xs:element name="IllegalArgumentException"
  22. type="tns:IllegalArgumentException" />
  23. <xs:complexType name="IllegalArgumentException">
  24. <xs:sequence />
  25. </xs:complexType>
  26. </xs:schema>
  27. </wsdl: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 元素只能出现一次。
  1. e.g.根据上面WSDL的SOAP request:
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test.cxf.bt.com/">
  3. <soapenv:Header/>
  4. <soapenv:Body>
  5. <test:testWS>
  6. <arg0>message passed in</arg0>
  7. </test:testWS>
  8. </soapenv:Body>
  9. </soapenv:Envelope>
  10. 返回的一个SOAP response:
  11. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  12. <soap:Body>
  13. <ns2:testWSResponse xmlns:ns2="http://test.cxf.bt.com/">
  14. <return>Returned: message passed in</return>
  15. </ns2:testWSResponse>
  16. </soap:Body>
  17. </soap:Envelope>
  18. 出错时候包含fault的response:
  19. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  20. <soap:Body>
  21. <ns1:Fault xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
  22. <faultcode>ns1:Server</faultcode>
  23. <faultstring>illegal argument.</faultstring>
  24. <detail>
  25. <ns1:IllegalArgumentException xmlns:ns1="http://test.cxf.bt.com/"/>
  26. </detail>
  27. </ns1:Fault>
  28. </soap:Body>
  29. </soap:Envelope>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值