赞
踩
Web服务(Web Service)是一种Web应用开发技术,用XML描述、发布、发现Web服务。它可以跨平台、进行分布式部署。
Web服务包含了一套标准,例如SOAP、WSDL、UDDI,定义了应用程序如何在Web上实现互操作。
Web服务的服务端和客户端使用简单对象访问协议(SOAP)进行通信,通信的双方可以使用不同的编程语言、运行在不同的平台上。
SOAP是一个基于XML的轻量级的协议,用于在去中心化的、分布式环境中交换结构化的、有类型的信息。是 Web服务的通信协议。
SOAP本身没有定义任何应用语义(例如一种编程模型)或者定义实现特有的语义,它只是通过提供一个模块化的包模型、通过一种编码机制(用于对模块内的数据编码),来定义一种简单的机制,用来表达应用语义。这样就使得SOAP可以用在各种各样的系统中,从消息系统到远程过程调用(RPC)。
一个SOAP消息用于请求一个web服务,例如远程过程调用(RPC)。但SOAP不仅仅、也不是必须用于RPC。
SOAP协议运行在其它协议之上,例如HTTP协议。
SOAP版本:SOAP 1.1、SOAP 1.2,两者都是W3C的标准。Web服务部署的时候可以两个版本都支持。
SOAP 1.1协议:https://www.w3.org/TR/2000/NOTE-SOAP-20000508/
SOAP 1.1包含如下三个部分:
一个SOAP消息就是一个XML文档,其中必须包含一个SOAP envelope和一个SOAP body,一个可选的SOAP header。
SOAP 1.1不依赖于传输协议,可以和多种传输协议结合,例如HTTP协议、java消息服务(JMS)。
SOAP独立于操作系统、不绑定某一个编程语言或者组件技术。
只要客户端能发出XML消息就行,用什么技术实现客户端无关紧要。类似地,服务可以用任何语言来实现,只要服务能处理SOAP消息就行。客户端和服务端可以运行在任何合适的平台上。
本示例来自SOAP 1.1协议:https://www.w3.org/TR/2000/NOTE-SOAP-20000508/。
更多示例请参考协议中的附录A:https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383539
备注:本示例中SOAP消息底层绑定了HTTP协议
示例中,一个GetLastTradePrice SOAP请求发送给StockQuote 服务。请求有一个字符串类型的参数(ticker symbol),在 SOAP 响应中返回一个float类型的值。
内嵌到HTTP 请求中的SOAP请求消息:
POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m="Some-URI">
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
内嵌到HTTP 响应中的SOAP 响应消息:
HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
<SOAP-ENV:Body>
<m:GetLastTradePriceResponse xmlns:m="Some-URI">
<Price>34.5</Price>
</m:GetLastTradePriceResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP 1.2包含三个部分、另外包含一些断言和一套测试集:
Part 0:入门https://www.w3.org/TR/soap12-part0/
描述了有代表性的SOAP消息结构和消息交互模式,通过各种使用场景描述了SOAP的特征,目的是为SOAP 1.2特征提供一个更加容易理解的指南,帮助技术开发者理解SOAP怎么使用。
Part 1:消息框架https://www.w3.org/TR/soap12-part1/
这部分提供了一个处理模型、一个扩展性模型、一个底层协议绑定框架、和SOAP消息结构。
Part 2:附加部分https://www.w3.org/TR/soap12-part2/
描述了SOAP数据模型、SOAP编码、SOAP RPC表示、描述特性和绑定的惯例、SOAP提供的消息交互模式、SOAP HTTP绑定。
本示例来自SOAP 1.2版本的part0:https://www.w3.org/TR/soap12-part0/
备注:本示例只演示SOAP消息,而暂时不管底层绑定了什么协议。
例1:从旅行预订应用 -》旅行预订服务,发起旅行预订请求
<?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> <m:reservation xmlns:m="http://travelcompany.example.org/reservation" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <m:reference>uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d</m:reference> <m:dateAndTime>2001-11-29T13:20:00.000-05:00</m:dateAndTime> </m:reservation> <n:passenger xmlns:n="http://mycompany.example.com/employees" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <n:name>Åke Jógvan Øyvind</n:name> </n:passenger> </env:Header> <env:Body> <p:itinerary xmlns:p="http://travelcompany.example.org/reservation/travel"> <p:departure> <p:departing>New York</p:departing> <p:arriving>Los Angeles</p:arriving> <p:departureDate>2001-12-14</p:departureDate> <p:departureTime>late afternoon</p:departureTime> <p:seatPreference>aisle</p:seatPreference> </p:departure> <p:return> <p:departing>Los Angeles</p:departing> <p:arriving>New York</p:arriving> <p:departureDate>2001-12-20</p:departureDate> <p:departureTime>mid-morning</p:departureTime> <p:seatPreference/> </p:return> </p:itinerary> <q:lodging xmlns:q="http://travelcompany.example.org/reservation/hotels"> <q:preference>none</q:preference> </q:lodging> </env:Body> </env:Envelope>
例2:从旅行预订服务 -》 旅行预订应用,返回响应,提供机场列表供选择
<?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> <m:reservation xmlns:m="http://travelcompany.example.org/reservation" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <m:reference>uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d</m:reference> <m:dateAndTime>2001-11-29T13:35:00.000-05:00</m:dateAndTime> </m:reservation> <n:passenger xmlns:n="http://mycompany.example.com/employees" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <n:name>Åke Jógvan Øyvind</n:name> </n:passenger> </env:Header> <env:Body> <p:itineraryClarification xmlns:p="http://travelcompany.example.org/reservation/travel"> <p:departure> <p:departing> <p:airportChoices> JFK LGA EWR </p:airportChoices> </p:departing> </p:departure> <p:return> <p:arriving> <p:airportChoices> JFK LGA EWR </p:airportChoices> </p:arriving> </p:return> </p:itineraryClarification> </env:Body> </env:Envelope>
例3:从旅行预订应用 -》 旅行预订服务,选择了机场
<?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> <m:reservation xmlns:m="http://travelcompany.example.org/reservation" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <m:reference>uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d</m:reference> <m:dateAndTime>2001-11-29T13:36:50.000-05:00</m:dateAndTime> </m:reservation> <n:passenger xmlns:n="http://mycompany.example.com/employees" env:role="http://www.w3.org/2003/05/soap-envelope/role/next" env:mustUnderstand="true"> <n:name>Åke Jógvan Øyvind</n:name> </n:passenger> </env:Header> <env:Body> <p:itinerary xmlns:p="http://travelcompany.example.org/reservation/travel"> <p:departure> <p:departing>LGA</p:departing> </p:departure> <p:return> <p:arriving>EWR</p:arriving> </p:return> </p:itinerary> </env:Body> </env:Envelope>
https://www.w3.org/TR/soap12-part0/#L4697
WSDL是XML格式,用于描述Web服务。
WSDL 1.1版本W3C发布出来仅仅用于讨论,但没有批准。
WSDL 2.0版本是被W3C批准的正式版本。
https://www.w3.org/TR/2001/NOTE-wsdl-20010315
包含如下几部分:
样例来自:https://www.w3.org/TR/2001/NOTE-wsdl-20010315#_wsdl
<?xml version="1.0"?> <definitions name="StockQuote" targetNamespace="http://example.com/stockquote.wsdl" xmlns:tns="http://example.com/stockquote.wsdl" xmlns:xsd1="http://example.com/stockquote.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="http://example.com/stockquote.xsd" xmlns="http://www.w3.org/2000/10/XMLSchema"> <element name="TradePriceRequest"> <complexType> <all> <element name="tickerSymbol" type="string"/> </all> </complexType> </element> <element name="TradePrice"> <complexType> <all> <element name="price" type="float"/> </all> </complexType> </element> </schema> </types> <message name="GetLastTradePriceInput"> <part name="body" element="xsd1:TradePriceRequest"/> </message> <message name="GetLastTradePriceOutput"> <part name="body" element="xsd1:TradePrice"/> </message> <portType name="StockQuotePortType"> <operation name="GetLastTradePrice"> <input message="tns:GetLastTradePriceInput"/> <output message="tns:GetLastTradePriceOutput"/> </operation> </portType> <binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="GetLastTradePrice"> <soap:operation soapAction="http://example.com/GetLastTradePrice"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="StockQuoteService"> <documentation>My first service</documentation> <port name="StockQuotePort" binding="tns:StockQuoteBinding"> <soap:address location="http://example.com/stockquote"/> </port> </service> </definitions>
WSDL 2.0包括3部分:
样例来自:https://www.w3.org/TR/wsdl20-primer/#basics-greath-scenario
<?xml version="1.0" encoding="utf-8" ?> <description xmlns="http://www.w3.org/ns/wsdl" targetNamespace= "http://greath.example.com/2004/wsdl/resSvc" xmlns:tns= "http://greath.example.com/2004/wsdl/resSvc" xmlns:ghns = "http://greath.example.com/2004/schemas/resSvc" xmlns:wsoap= "http://www.w3.org/ns/wsdl/soap" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsdlx= "http://www.w3.org/ns/wsdl-extensions"> <documentation> This document describes the GreatH Web service. Additional application-level requirements for use of this service -- beyond what WSDL 2.0 is able to describe -- are available at http://greath.example.com/2004/reservation-documentation.html </documentation> <types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://greath.example.com/2004/schemas/resSvc" xmlns="http://greath.example.com/2004/schemas/resSvc"> <xs:element name="checkAvailability" type="tCheckAvailability"/> <xs:complexType name="tCheckAvailability"> <xs:sequence> <xs:element name="checkInDate" type="xs:date"/> <xs:element name="checkOutDate" type="xs:date"/> <xs:element name="roomType" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="checkAvailabilityResponse" type="xs:double"/> <xs:element name="invalidDataError" type="xs:string"/> </xs:schema> </types> <interface name = "reservationInterface" > <fault name = "invalidDataFault" element = "ghns:invalidDataError"/> <operation name="opCheckAvailability" pattern="http://www.w3.org/ns/wsdl/in-out" style="http://www.w3.org/ns/wsdl/style/iri" wsdlx:safe = "true"> <input messageLabel="In" element="ghns:checkAvailability" /> <output messageLabel="Out" element="ghns:checkAvailabilityResponse" /> <outfault ref="tns:invalidDataFault" messageLabel="Out"/> </operation> </interface> <binding name="reservationSOAPBinding" interface="tns:reservationInterface" type="http://www.w3.org/ns/wsdl/soap" wsoap:protocol="http://www.w3.org/2003/05/soap/bindings/HTTP/"> <fault ref="tns:invalidDataFault" wsoap:code="soap:Sender"/> <operation ref="tns:opCheckAvailability" wsoap:mep="http://www.w3.org/2003/05/soap/mep/soap-response"/> </binding> <service name="reservationService" interface="tns:reservationInterface"> <endpoint name="reservationEndpoint" binding="tns:reservationSOAPBinding" address ="http://greath.example.com/2004/reservation"/> </service> </description>
备注:OASIS已经宣布关闭了UDDI技术委员
UDDI基于XML,规定如何注册、分发、发现 Web服务。发布的标准有 V2和V3。
UDDI Version 2 OASIS标准包含如下部分:
https://www.oasis-open.org/committees/uddi-spec/doc/tcspecs.htm#uddiv2
UDDI Version 3 OASIS标准包含如下部分:
https://www.oasis-open.org/committees/uddi-spec/doc/tcspecs.htm#uddiv3
SAAJ提供了利用Java编程模型在互联网上发送XML SOAP消息的标准方法,SOAP消息可以携带附件(例如附件是二进制形式)。SAAJ用来处理 SOAP消息,例如读、写、发送和接收。
SAAJ 1.2 只支持SOAP 1.1消息,而SAAJ 1.3支持SOAP 1.1 和SOAP 1.2 消息。
SAAJ 1.3向后兼容SAAJ 1.2,SAAJ 1.4向后兼容SAAJ 1.3。
SAAJ 1.3:
https://www.jcp.org/aboutJava/communityprocess/maintenance/jsr067/SAAJ1_3ChangeLog.html
SAAJ 1.4:
https://download.oracle.com/otn-pub/jcp/saaj-1_4-mrel4-eval-spec/saaj-1.4.pdf
Jakarta SOAP with Attachments 2.0:
https://jakarta.ee/specifications/soap-attachments/2.0/
Jakarta SOAP with Attachments 3.0:
https://jakarta.ee/specifications/soap-attachments/3.0/
Web服务策略用来表示Web服务的能力和需求,是供机器阅读的语言。
Web服务策略提供了一种机制,用来表示Web服务的能力和需求一致的结合,决定策略的兼容性,命名和引用策略,将Web服务元数据结构(例如service、endpoint 和operation)和策略关联。
Web服务策略是一门简单的语言,有四个元素(Policy, All, ExactlyOne 和 PolicyReference ) 和两个属性(wsp:Optional 和 wsp:Ignorable)。
例如文档中给出的策略示例(https://www.w3.org/TR/ws-policy-primer/):
<All>
<wsam:Addressing>…</wsam:Addressing>
<ExactlyOne>
<sp:TransportBinding>…</sp:TransportBinding>
<sp:AsymmetricBinding>…</sp:AsymmetricBinding>
</ExactlyOne>
</All>
<All>
<mtom:OptimizedMimeSerialization wsp:Optional="true"/>
<wsam:Addressing>…</wsam:Addressing>
<ExactlyOne>
<sp:TransportBinding>…</sp:TransportBinding>
<sp:AsymmetricBinding>…</sp:AsymmetricBinding>
</ExactlyOne>
</All>
<sp:TransportBinding> <Policy> <sp:TransportToken> <Policy> <sp:HttpsToken> <wsp:Policy/> </sp:HttpsToken> </Policy> </sp:TransportToken> <sp:AlgorithmSuite> <Policy> <sp:Basic256Rsa15/> </Policy> </sp:AlgorithmSuite> … </Policy> </sp:TransportBinding>
Web Services Policy 1.5版本包含如下几部分:
Web服务寻址提供了跟传输无关的访问Web服务和消息的机制。
示例:在SOAP 1.2消息中利用寻址属性(示例来自https://www.w3.org/TR/ws-addr-core/#tocRange):
(01) <S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing">
(02) <S:Header>
(03) <wsa:MessageID>http://example.com/6B29FC40-CA47-1067-B31D-00DD010662DA</wsa:MessageID>
(04) <wsa:ReplyTo>
(05) <wsa:Address>http://example.com/business/client1</wsa:Address>
(06) </wsa:ReplyTo>
(07) <wsa:To>http://example.com/fabrikam/Purchasing</wsa:To>
(08) <wsa:Action>http://example.com/fabrikam/SubmitPO</wsa:Action>
(09) </S:Header>
(10) <S:Body>
(11) ...
(12) </S:Body>
(13) </S:Envelope>
Web Services Security: SOAP Message Security 1.0 (WS-Security 2004) :
http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0.pdf
这是OASIS发布的规范,取代Web Service Security (WS-Security) Version 1.0、Web Services Security Addendum Version 1.0。
本规范包括如下章节:
https://download.oracle.com/otn-pub/jcp/jaxws-2_3-mrel5-eval-spec/jaxws-2.3.pdf
JAX-WS 2.0 规范是代替JAX-RPC 1.0的下一代Web服务API。JAX-RPC(The Java API for XML Based RPC)是基于XML的RPC(远程过程调用)的Java API。
JAX-WS 2.0 使用JAXB(The Java Architecture for XML Binding)进行XML数据绑定,所谓绑定就是指XML请求和XML响应如何映射成java对象。
本规范包括如下章节:
https://jcp.org/en/jsr/detail?id=222
https://download.oracle.com/otn-pub/jcp/jaxb-2_3-mrel3-eval-spec/JAXB-2.3-spec.pdf
JAXB可以根据XML Schema生成java类,也能根据java类生成XML Schema。XML数据绑定指定了XML请求和XML响应如何映射成java对象。
另外,支持三个主要操作:
解包的过程能够对照schema检查XML文档的正确性。
下面的图来自:https://download.oracle.com/otn-pub/jcp/jaxb-2_3-mrel3-eval-spec/JAXB-2.3-spec.pdf
定义了XML二进制优化打包规则,可以更加高效地序列化XML信息集。
下面是协议文档中给出的一个例子:
使用XOP处理之前的XML信息集(SOAP)
<soap:Envelope
xmlns:soap='http://www.w3.org/2003/05/soap-envelope'
xmlns:xmlmime='http://www.w3.org/2004/11/xmlmime'>
<soap:Body>
<m:data xmlns:m='http://example.org/stuff'>
<m:photo
xmlmime:contentType='image/png'>/aWKKapGGyQ=</m:photo>
<m:sig
xmlmime:contentType='application/pkcs7-signature'>Faa7vROi2VQ=</m:sig>
</m:data>
</soap:Body>
</soap:Envelope>
将XML信息集序列化XOP消息(SOAP)
MIME-Version: 1.0 Content-Type: Multipart/Related;boundary=MIME_boundary; type="application/xop+xml"; start="<mymessage.xml@example.org>"; startinfo="application/soap+xml; action=\"ProcessData\"" Content-Description: A SOAP message with my pic and sig in it --MIME_boundary Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml; action=\"ProcessData\"" Content-Transfer-Encoding: 8bit Content-ID: <mymessage.xml@example.org> <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xmlmime='http://www.w3.org/2004/11/xmlmime'> <soap:Body> <m:data xmlns:m='http://example.org/stuff'> <m:photo xmlmime:contentType='image/png'><xop:Include xmlns:xop='http://www.w3.org/2004/08/xop/include' href='cid:http://example.org/me.png'/></m:photo> <m:sig xmlmime:contentType='application/pkcs7-signature'><xop:Include xmlns:xop='http://www.w3.org/2004/08/xop/include' href='cid:http://example.org/my.hsh'/></m:sig> </m:data> </soap:Body> </soap:Envelope> --MIME_boundary Content-Type: image/png Content-Transfer-Encoding: binary Content-ID: <http://example.org/me.png> // binary octets for png --MIME_boundary Content-Type: application/pkcs7-signature Content-Transfer-Encoding: binary Content-ID: <http://example.org/my.hsh> // binary octets for signature --MIME_boundary--
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。