大家好,欢迎来到IT知识分享网。
简介
简单对象访问协议(SOAP:Simple Object Access Protocol)
总体介绍
一个SOAP消息用于请求一个web服务,例如远程过程调用(RPC)。但SOAP不仅仅、也不是必须用于RPC。
SOAP协议运行在其它协议之上,例如HTTP协议。
SOAP版本:SOAP 1.1、SOAP 1.2,两者都是W3C的标准。Web服务部署的时候可以两个版本都支持。
SOAP 1.1
协议介绍
SOAP 1.1协议:https://www.w3.org/TR/2000/NOTE-SOAP-/
SOAP 1.1包含如下三个部分:
- SOAP 信封(envelope),定义了一个总体框架,用于表达消息内是什么、谁应该处理这个消息、是可选的还是强制的。
- SOAP 编码规则(encoding rules),定义了一种序列化机制,用于交换应用定义的数据类型实例。
- SOAP RPC表达,定义了一个规则,用来表达远程过程调用(RPC)和响应。
一个SOAP消息就是一个XML文档,其中必须包含一个SOAP envelope和一个SOAP body,一个可选的SOAP header。
SOAP 1.1不依赖于传输协议,可以和多种传输协议结合,例如HTTP协议、java消息服务(JMS)。
消息示例(用于RPC的场景)
备注:本示例中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
协议介绍
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>
SOAP 1.1 和 SOAP 1.2之间的变化
https://www.w3.org/TR/soap12-part0/#L4697
Web服务描述语言(WSDL:Web Services Description Language)
简介
WSDL 1.1
介绍
https://www.w3.org/TR/2001/NOTE-wsdl-
包含如下几部分:
- 服务定义
- SOAP绑定
- HTTP GET 和 POST绑定
- MIME绑定
WSDL 1.1文档样例
样例来自:https://www.w3.org/TR/2001/NOTE-wsdl-#_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
介绍
WSDL 2.0包括3部分:
- part 0 入门:https://www.w3.org/TR/wsdl20-primer/
本部分包含WSDL 2.0基本知识、导入机制、扩展性和预先定义的扩展、杂项。 - Part 1 核心:https://www.w3.org/TR/wsdl20/
本部分包含组件模型、类型、模块化、文档、语言扩展性、定位 WSDL 2.0文档。 - Part 2 附件:https://www.w3.org/TR/wsdl20-adjuncts/
本部分包含预先定义的消息交互模式、预先定义的扩展、预先定义的行为样式、WSDL SOAP绑定扩展、WSDL HTTP绑定扩展。
WSDL 2.0文档样例
样例来自: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>
统一描述、发现和集成(UDDI:Universal Description, Discovery and Integration)
备注: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
支持带附件的SOAP消息Java接口(SAAJ:SOAP with Attachments API for Java interface)
SAAJ提供了利用Java编程模型在互联网上发送XML SOAP消息的标准方法,SOAP消息可以携带附件(例如附件是二进制形式)。SAAJ用来处理 SOAP消息,例如读、写、发送和接收。
主要的类和接口:
https://www.ibm.com/docs/en/was/9.0.5?topic=SSEQTP_9.0.5/com.ibm.websphere.nd.multiplatform.doc/ae/cwbs_saaj.htm
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服务策略(WS-Policy:Web Services Policy)
例如文档中给出的策略示例(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版本包含如下几部分:
- 入门:https://www.w3.org/TR/ws-policy-primer/
这部分不是一篇正式的规范。它提供了Web服务策略语言的介绍性描述。基本概念—策略表达式章节覆盖了Web服务策略的基本机制,高级概念—策略表达式章节针对策略实现者和断言作者提供了更深层次的资料,策略语言版本章节提供了策略语言版本管理的例子和最佳实践。 - 框架:https://www.w3.org/TR/ws-policy/
这部分提供一个通用目的模型和对应的语法,来描述基于Web服务的系统中实体的策略。定义了一套基本的结构,可以被其它的Web服务规范使用和扩展,来描述更广范围的要求和能力。 - 附件:https://www.w3.org/TR/ws-policy-attach/
这部分定义了两个通用目的关联策略和目标的机制,也定义了这些通用目的的机制怎么来使策略和WSDL 和 UDDI 描述关联。
Web服务寻址(WS-Addressing:Web Services Addressing)
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服务寻址 1.0 – Core:https://www.w3.org/TR/ws-addr-core/
本部分包含端点引用、消息寻址属性、安全考虑。
Web服务安全(WS-Security)
- 消息保护机制
- ID引用
- 安全头
- 安全标识
- 标识引用
- 签名
- 加密
- 安全时间戳
- 扩展样例
- 错误处理
- 安全考虑
- 互操作性
- 隐私考虑
JAX-WS(Java API for XML-Based Web Services)
https://download.oracle.com/otn-pub/jcp/jaxws-2_3-mrel5-eval-spec/jaxws-2.3.pdf
本规范包括如下章节:
- 介绍
- WSDL 1.1 到Java的 映射
- Java 到 WSDL 1.1的映射
- 客户端API
- 服务API
- 核心API
- 注解
- 定制化
- Handler框架
- SOAP绑定
- HTTP绑定
JAXB(Java Architecture for XML Binding)
- 将XML数据解包为java 对象的内容树。
- 将java内容树打包回XML文档。
- XML文档表示和java内容树的绑定。
XOP(XML-binary Optimized Packaging)
https://www.w3.org/TR/xop10/
定义了XML二进制优化打包规则,可以更加高效地序列化XML信息集。
<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="<>"; 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: <> <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--
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/130317.html