Web Services---快速入门之详解

一、Web Services 简介

Web Services 可使您的应用程序成为 Web 应用程序。
Web Services 通过 Web 进行发布、查找和使用。

  • 什么是Web Services?

Web Services 是应用程序组件
Web Services 使用开放协议进行通信
Web Services 是独立的(self-contained)并可自我描述
Web Services 可通过使用UDDI来发现
Web Services 可被其他应用程序使用
XML 是 Web Services 的基础

  • 它如何工作?

基础的 Web Services 平台是 XML + HTTP。
HTTP 协议是最常用的因特网协议。
XML 提供了一种可用于不同的平台和编程语言之间的语言。
Web services 平台的元素:
SOAP (简易对象访问协议)
UDDI (通用描述、发现及整合)
WSDL (Web services 描述语言)

二、Web Services 平台元素

Web Services 拥有三种基本的元素:SOAP、WSDL 以及 UDDI。

  • SOAP 简介

SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。
或者更简单地说:SOAP 是用于访问网络服务的协议。

  • 什么是 SOAP?

SOAP 指简易对象访问协议
SOAP 是一种通信协议
SOAP 用于应用程序之间的通信
SOAP 是一种用于发送消息的格式
SOAP 被设计用来通过因特网进行通信
SOAP 独立于平台
SOAP 独立于语言
SOAP 基于 XML
SOAP 很简单并可扩展
SOAP 允许您绕过防火墙
SOAP 将被作为 W3C 标准来发展

  • 为什么使用 SOAP?

对于应用程序开发来说,使程序之间进行因特网通信是很重要的。
目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 HTTP 不是为此设计的。RPC 会产生兼容性以及安全问题;防火墙和代理服务器通常会阻止此类流量。
通过 HTTP 在应用程序间通信是更好的方法,因为 HTTP 得到了所有的因特网浏览器及服务器的支持。SOAP 就是被创造出来完成这个任务的。
SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

  • SOAP 语法

SOAP 构建模块
一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:
必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息
可选的 Header 元素,包含头部信息
必需的 Body 元素,包含所有的调用和响应信息
可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
所有以上的元素均被声明于针对 SOAP 封装的默认命名空间中:
http://www.w3.org/2001/12/soap-envelope
以及针对 SOAP 编码和数据类型的默认命名空间:
http://www.w3.org/2001/12/soap-encoding

  • 语法规则

这里是一些重要的语法规则:
SOAP 消息必须用 XML 来编码
SOAP 消息必须使用 SOAP Envelope 命名空间
SOAP 消息必须使用 SOAP Encoding 命名空间
SOAP 消息不能包含 DTD 引用
SOAP 消息不能包含 XML 处理指令
SOAP 消息的基本结构

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
...
</soap:Header>

<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>

</soap:Envelope>

SOAP Envelope 元素
强制使用的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。
SOAP Envelope 元素
必需的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。它可把 XML 文档定义为 SOAP 消息。
实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  ...
  Message information goes here
  ...
</soap:Envelope>

xmlns:soap 命名空间
SOAP 消息必须拥有与命名空间 “http://www.w3.org/2001/12/soap-envelope” 相关联的一个 Envelope 元素。
如果使用了不同的命名空间,应用程序会发生错误,并抛弃此消息。
encodingStyle 属性
SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
SOAP 消息没有默认的编码方式。
语法

soap:encodingStyle="URI"

实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  ...
  Message information goes here
  ...
</soap:Envelope>

SOAP Header 元素
可选的 SOAP Header 元素包含头部信息。
SOAP Header 元素
可选的 SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。
如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。
注意: 所有 Header 元素的直接子元素必须是合格的命名空间。

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

上面的例子包含了一个带有一个 “Trans” 元素的头部,它的值是 234,此元素的 “mustUnderstand” 属性的值是 “1”。
SOAP 在默认的命名空间中 (“http://www.w3.org/2001/12/soap-envelope”) 定义了三个属性。
这三个属性是:actor、 mustUnderstand 以及 encodingStyle。这些被定义在 SOAP 头部的属性可定义容器如何对 SOAP 消息进行处理。
mustUnderstand 属性
SOAP 的 mustUnderstand 属性可用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的。
假如您向 Header 元素的某个子元素添加了 "mustUnderstand=“1”,则它可指示处理此头部的接收者必须认可此元素。假如此接收者无法认可此元素,则在处理此头部时必须失效。
语法

soap:mustUnderstand="0|1"

实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

actor 属性
通过沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者。并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,不过,另一个方面,也许打算传送给消息路径上的一个或多个端点。
SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点。
语法

soap:actor="URI"

实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:actor="http://www.w3schools.com/appml/">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

encodingStyle 属性
SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
SOAP 消息没有默认的编码方式。
语法

soap:encodingStyle="URI"

SOAP Header 元素
可选的 SOAP Header 元素包含头部信息。
SOAP Header 元素
可选的 SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。
如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。
注意: 所有 Header 元素的直接子元素必须是合格的命名空间。

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

上面的例子包含了一个带有一个 “Trans” 元素的头部,它的值是 234,此元素的 “mustUnderstand” 属性的值是 “1”。
SOAP 在默认的命名空间中 (“http://www.w3.org/2001/12/soap-envelope”) 定义了三个属性。
这三个属性是:actor、 mustUnderstand 以及 encodingStyle。这些被定义在 SOAP 头部的属性可定义容器如何对 SOAP 消息进行处理。
mustUnderstand 属性
SOAP 的 mustUnderstand 属性可用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的。
假如您向 Header 元素的某个子元素添加了 "mustUnderstand=“1”,则它可指示处理此头部的接收者必须认可此元素。假如此接收者无法认可此元素,则在处理此头部时必须失效。
语法

soap:mustUnderstand="0|1"

实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

actor 属性
通过沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者。并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,不过,另一个方面,也许打算传送给消息路径上的一个或多个端点。
SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点。
语法

soap:actor="URI"

实例

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:actor="http://www.w3schools.com/appml/">234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope>

encodingStyle 属性
SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
SOAP 消息没有默认的编码方式。
语法

soap:encodingStyle="URI"

SOAP Fault 元素
SOAP Fault 元素用于存留 SOAP 消息的错误和状态信息。
SOAP Fault 元素
可选的 SOAP Fault 元素用于指示错误消息。
如果已提供了 Fault 元素,则它必须是 Body 元素的子元素。在一条 SOAP 消息中,Fault 元素只能出现一次。
SOAP 的 Fault 元素拥有下列子元素:
子元素 描述
供识别故障的代码
可供人阅读的有关故障的说明
有关是谁引发故障的信息
存留涉及 Body 元素的应用程序专用错误信息
SOAP Fault 代码
在下面定义的 faultcode 值必须用于描述错误时的 faultcode 元素中:
错误 描述
VersionMismatch SOAP Envelope 元素的无效命名空间被发现
MustUnderstand Header 元素的一个直接子元素(带有设置为 “1” 的 mustUnderstand 属性)无法被理解。
Client 消息被不正确地构成,或包含了不正确的信息。
Server 服务器有问题,因此无法处理进行下去。
SOAP HTTP 协议
HTTP 协议
HTTP 在 TCP/IP 之上进行通信。HTTP 客户机使用 TCP 连接到 HTTP 服务器。在建立连接之后,客户机可向服务器发送 HTTP 请求消息:

POST /item HTTP/1.1
Host: 189.123.255.239
Content-Type: text/plain
Content-Length: 200

随后服务器会处理此请求,然后向客户机发送一个 HTTP 响应。此响应包含了可指示请求状态的状态代码:

200 OK
Content-Type: text/plain
Content-Length: 200

在上面的例子中,服务器返回了一个 200 的状态代码。这是 HTTP 的标准成功代码。
假如服务器无法对请求进行解码,它可能会返回类似这样的信息:

400 Bad Request
Content-Length: 0
SOAP HTTP Binding

SOAP 方法指的是遵守 SOAP 编码规则的 HTTP 请求/响应。
HTTP + XML = SOAP
SOAP 请求可能是 HTTP POST 或 HTTP GET 请求。
HTTP POST 请求规定至少两个 HTTP 头:Content-Type 和 Content-Length。
Content-Type
SOAP 的请求和响应的 Content-Type 头可定义消息的 MIME 类型,以及用于请求或响应的 XML 主体的字符编码(可选)。
语法

Content-Type: MIMEType; charset=character-encoding

实例

POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8

Content-Length
SOAP 的请求和响应的 Content-Length 头规定请求或响应主体的字节数。
语法

Content-Length: bytes

实例

POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 250

SOAP 实例
一个 SOAP 实例
在下面的例子中,一个 GetStockPrice 请求被发送到了服务器。此请求有一个 StockName 参数,而在响应中则会返回一个 Price 参数。此功能的命名空间被定义在此地址中: “http://www.example.org/stock
SOAP 请求:

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>

SOAP 响应:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>
  • 什么是 WSDL?

WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言。
WSDL 指网络服务描述语言
WSDL 使用 XML 编写
WSDL 是一种 XML 文档
WSDL 用于描述网络服务
WSDL 也可用于定位网络服务
WSDL 还不是 W3C 标准

  • WSDL 简介

WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言。

  • 什么是 WSDL?

WSDL 指网络服务描述语言
WSDL 使用 XML 编写
WSDL 是一种 XML 文档
WSDL 用于描述网络服务
WSDL 也可用于定位网络服务
WSDL 还不是 W3C 标准
WSDL 可描述网络服务(Web Services)
WSDL 指网络服务描述语言 (Web Services Description Language)。
WSDL 是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作(或方法)。

  • WSDL 文档

WSDL 文档仅仅是一个简单的 XML 文档。
它包含一系列描述某个 web service 的定义。

  • WSDL 文档结构

WSDL 文档是利用这些主要的元素来描述某个 web service 的:
元素 定义
web service 执行的操作
web service 使用的消息
web service 使用的数据类型
web service 使用的通信协议
一个 WSDL 文档的主要结构是类似这样的:

<definitions>

<types>
  data type definitions........
</types>

<message>
  definition of the data being communicated....
</message>

<portType>
  set of operations......
</portType>

<binding>
  protocol and data format specification....
</binding>

</definitions>

WSDL 文档可包含其它的元素,比如 extension 元素,以及一个 service 元素,此元素可把若干个 web services 的定义组合在一个单一的 WSDL 文档中。
WSDL 端口
元素是最重要的 WSDL 元素。
它可描述一个 web service、可被执行的操作,以及相关的消息。
可以把 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。
WSDL 消息
元素定义一个操作的数据元素。
每个消息均由一个或多个部件组成。可以把这些部件比作传统编程语言中一个函数调用的参数。
WSDL types
元素定义 web service 使用的数据类型。
为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。
WSDL Bindings
元素为每个端口定义消息格式和协议细节。
WSDL 实例
这是某个 WSDL 文档的简化的片段:

<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

在这个例子中, 元素把 “glossaryTerms” 定义为某个端口的名称,把 “getTerm” 定义为某个操作的名称。
操作 “getTerm” 拥有一个名为 “getTermRequest” 的输入消息,以及一个名为 “getTermResponse” 的输出消息。
元素可定义每个消息的部件,以及相关联的数据类型。
对比传统的编程,glossaryTerms 是一个函数库,而 “getTerm” 是带有输入参数 “getTermRequest” 和返回参数 getTermResponse 的一个函数。
WSDL 端口
元素是最重要的 WSDL 元素。
WSDL 端口
元素是最重要的 WSDL 元素。
它可描述一个 web service、可被执行的操作,以及相关的消息。
可以把 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。
操作类型
请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:
类型 定义
One-way 此操作可接受消息,但不会返回响应。
Request-response 此操作可接受一个请求并会返回一个响应
Solicit-response 此操作可发送一个请求,并会等待一个响应。
Notification 此操作可发送一条消息,但不会等待响应。

One-Way 操作
一个 one-way 操作的例子:

<message name="newTermValues">
  <part name="term" type="xs:string"/>
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="setTerm">
    <input name="newTerm" message="newTermValues"/>
  </operation>
</portType >

在这个例子中,端口 “glossaryTerms” 定义了一个名为 “setTerm” 的 one-way 操作。
这个 “setTerm” 操作可接受新术语表项目消息的输入,这些消息使用一条名为 “newTermValues” 的消息,此消息带有输入参数 “term” 和 “value”。不过,没有为这个操作定义任何输出。
Request-Response 操作
一个 request-response 操作的例子:

<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

在这个例子中,端口 “glossaryTerms” 定义了一个名为 “getTerm” 的 request-response 操作。
“getTerm” 操作会请求一个名为 “getTermRequest” 的输入消息,此消息带有一个名为 “term” 的参数,并将返回一个名为 “getTermResponse” 的输出消息,此消息带有一个名为 “value” 的参数。
WSDL 绑定
WSDL 绑定可为 web service 定义消息格式和协议细节。
绑定到 SOAP
一个 请求 - 响应 操作的例子:

<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

<binding type="glossaryTerms" name="b1">
   <soap:binding style="document"
   transport="http://schemas.xmlsoap.org/soap/http" />
   <operation>
     <soap:operation soapAction="http://example.com/getTerm"/>
     <input><soap:body use="literal"/></input>
     <output><soap:body use="literal"/></output>
  </operation>
</binding>

binding 元素有两个属性 - name 属性和 type 属性。
name 属性定义 binding 的名称,而 type 属性指向用于 binding 的端口,在这个例子中是 “glossaryTerms” 端口。
soap:binding 元素有两个属性 - style 属性和 transport 属性。
style 属性可取值 “rpc” 或 “document”。在这个例子中我们使用 document。transport 属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP。
operation 元素定义了每个端口提供的操作符。
对于每个操作,相应的 SOAP 行为都需要被定义。同时您必须如何对输入和输出进行编码。在这个例子中我们使用了 “literal”。

  • WSDL UDDI

UDDI 是一种目录服务,企业可以使用它对 Web services 进行注册和搜索。
UDDI,英文为 “Universal Description, Discovery and Integration”,可译为"通用描述、发现与集成服务"。

  • 什么是 UDDI?

UDDI 是一个独立于平台的框架,用于通过使用 Internet 来描述服务,发现企业,并对企业服务进行集成。
UDDI 指的是通用描述、发现与集成服务
UDDI 是一种用于存储有关 web services 的信息的目录。
UDDI 是一种由 WSDL 描述的 web services 界面的目录。
UDDI 经由 SOAP 进行通信
UDDI 被构建入了微软的 .NET 平台

  • UDDI 基于什么?

UDDI 使用 W3C 和 IETF* 的因特网标准,比如 XML、HTTP 和 DNS 协议。
UDDI 使用 WSDL 来描述到达 web services 的界面
此外,通过采用 SOAP,还可以实现跨平台的编程特性,大家知道,SOAP 是 XML 的协议通信规范,可在 W3C 的网站找到相关的信息。
*注释:IETF - Internet Engineering Task Force

  • UDDI 的好处

任何规模的行业或企业都能得益于 UDDI。
在 UDDI 之前,还不存在一种 Internet 标准,可以供企业为它们的企业和伙伴提供有关其产品和服务的信息。也不存在一种方法,来集成到彼此的系统和进程中。
UDDI 规范帮助我们解决的问题:
使得在成百万当前在线的企业中发现正确的企业成为可能
定义一旦首选的企业被发现后如何启动商业
扩展新客户并增加对目前客户的访问
扩展销售并延伸市场范围
满足用户驱动的需要,为在全球 Internet 经济中快速合作的促进来清除障碍

  • UDDI 如何被使用

假如行业发布了一个用于航班比率检测和预订的 UDDI 标准,航空公司就可以把它们的服务注册到一个 UDDI 目录中。然后旅行社就能够搜索这个 UDDI 目录以找到航空公司预订界面。当此界面被找到后,旅行社就能够立即与此服务进行通信,这样由于它使用了一套定义良好的预订界面。

  • 谁在支持 UDDI?

UDDI 是一个跨行业的研究项目,由所有主要的平台和软件提供商驱动,比如:Dell, Fujitsu, HP, Hitachi, IBM, Intel, Microsoft, Oracle, SAP, 以及 Sun, 它既是一个市场经营者的团体,也是一个电子商务的领导者。
已有数百家公司参与了这个 UDDI 团体。
描述于 W3C 工作草案的完整 WSDL 1.2 语法已列在下面:

<wsdl:definitions name="nmtoken"? targetNamespace="uri">

    <import namespace="uri" location="uri"/> *
    
    <wsdl:documentation .... /> ?

    <wsdl:types> ?
        <wsdl:documentation .... /> ?
        <xsd:schema .... /> *
    </wsdl:types>

    <wsdl:message name="ncname"> *
        <wsdl:documentation .... /> ?
        <part name="ncname" element="qname"? type="qname"?/> *
    </wsdl:message>

    <wsdl:portType name="ncname"> *
        <wsdl:documentation .... /> ?
        <wsdl:operation name="ncname"> *
            <wsdl:documentation .... /> ?
            <wsdl:input message="qname"> ?
                <wsdl:documentation .... /> ?
            </wsdl:input>
            <wsdl:output message="qname"> ?
                <wsdl:documentation .... /> ?
            </wsdl:output>
            <wsdl:fault name="ncname" message="qname"> *
                <wsdl:documentation .... /> ?
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:serviceType name="ncname"> *
        <wsdl:portType name="qname"/> +
    </wsdl:serviceType>

    <wsdl:binding name="ncname" type="qname"> *
        <wsdl:documentation .... /> ?
        <-- binding details --> *
        <wsdl:operation name="ncname"> *
            <wsdl:documentation .... /> ?
            <-- binding details --> *
            <wsdl:input> ?
                <wsdl:documentation .... /> ?
                <-- binding details -->
            </wsdl:input>
            <wsdl:output> ?
                <wsdl:documentation .... /> ?
                <-- binding details --> *
            </wsdl:output>
            <wsdl:fault name="ncname"> *
                <wsdl:documentation .... /> ?
                <-- binding details --> *
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="ncname" serviceType="qname"> *
        <wsdl:documentation .... /> ?
        <wsdl:port name="ncname" binding="qname"> *
            <wsdl:documentation .... /> ?
            <-- address details -->
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>

三、Web Service实例

  • 基于eclipse开发服务接口以及调用
    首先,我们在eclipse中像建立一个web项目一样,new->Dynamic Web Project,这里我取名为helloService
    在这里插入图片描述
    接着,我们先建一个需要暴露给外部的方法,
package service;
 
public class HelloService {
	public String say(String name) throws InterruptedException{
		return "hello "+name;
	}
}

然后呢,右击这个项目,new -> other->web services->webservice
在这里插入图片描述
选择需要暴露的实现,service.HelloService,然后选择发布
在这里插入图片描述
我们是通过tomcat发布的,直接start即可。常用的框架有,cxf、 axis、 axis2等,这里选择了axis

发布之后,我们打开网页输入地址即可打开它的wsdl:http://localhost:8280/helloService/services/HelloService?wsdl

前面的地址,在helloService\WebContent\wsdl\HelloService.wsdl下可以看到,
在这里插入图片描述
在这里插入图片描述
现在服务端已经建立。

  • webService客户端开发

接下来,我们就需要使用client去连这个Webservice服务了,新建一个java工程(都可以)
在这里插入图片描述
然后新建一个Webservice client就可以,输入wsdl地址,finish即可
在这里插入图片描述
然后可以看到目录下的Webservice java类,
在这里插入图片描述
我们新建一个test,去测试以下

package test;
 
import java.rmi.RemoteException;
 
import service.HelloService;
import service.HelloServiceProxy;
 
public class Test {
 
	public static void main(String[] args) throws RemoteException {
		HelloServiceProxy helloPxy = new HelloServiceProxy();
		HelloService service = helloPxy.getHelloService();
		String res = service.say("yyf");
		System.out.println(res);
	}
 
}

在这里插入图片描述
基于eclipse的Webservice的入门结束。
客户端超时断开连接设置:
HelloServiceSoapBindingStub->

protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
    	 _call.setTimeout(1000);//设置超时时间
  • 基于jdk开发webservice接口以及调用
    首先定义一个天气预报的接口,Weather
@WebService
public interface Weather {
    String queryWeather();
}

定义一个实现类,实现该接口

@WebService
public class WeatherImpl implements Weather{
	@Override
    public String queryWeather() {
        return "今日天气为晴,偏北风二到三级";
    }
}

通过JAX-WS提供的Endpoint来发布webservice

public class MainServer {
	public static void main(String[] args) {
		// 首先创建webservice服务提供类的实例
		 WeatherImpl implementor = new WeatherImpl (); 
		 String address = "http://localhost:8080/Weather";
		 Endpoint.publish(address, implementor);
		 System.out.println("webservice暴露成功!");
	}
}

访问http://localhost:8080/Weather,结果如下:
在这里插入图片描述
表示发布成功。
当然如果只是测试接口,使用soapUi测试也是可以的且使用简单.
调用一个发布的webservice接口方法 可以同上也可以如下:
(1)、开发步骤
A、在工作空间创建用于存放使用wsimport命令生成的客户端代码的java工程
在这里插入图片描述
B、使用jdk提供的wsimport命令生成客户端代码
在这里插入图片描述
● wsimport命令是jdk提供的,作用是根据使用说明书生成客户端代码,wsimport只支持SOAP1.1客户端的生成

● wsimport常用参数

-d:默认参数,用于生成.class文件
-s:生成.java文件
-p:指定生成java文件的包名,不指定则为WSDL说明书中namespace值得倒写
在这里插入图片描述
C、在doc窗口进入java工程项目的src目录,执行wsimport命令
在这里插入图片描述
D、在Eclipse中刷新java项目,将生成的客户端代码copy到客户端工程中
在这里插入图片描述
E、创建服务视图,类名从标签的name属性获取
在这里插入图片描述
F、获取服务实现类,视图实例调用getProt()方法,实现类的类名从portType的name属性获取
在这里插入图片描述
G、调用查询方法,方法名从portType下的operation标签的name属性获取
在这里插入图片描述


package com.webservice.client;  
  
import com.webservice.jaxws.WeatherServiceImpl;  
import com.webservice.jaxws.WeatherServiceImplService;  
  
public class Client {  
  
    public static void main(String[] args) {  
        //创建视图  
        WeatherServiceImplService wsis = new WeatherServiceImplService();  
        //获取服务实现类  
        WeatherServiceImpl wsi = wsis.getPort(WeatherServiceImpl.class);  
        //调用查询方法  
        String weather = wsi.queryWeather("北京");  
        System.out.println(weather);  
    }  
} 

使用CXF开发WebService客户端
执行如下命令(找到webService的客户端的工作空间执行)在这里插入图片描述
刷新客户端项目,可看到如下生成的代码:
在这里插入图片描述

package hjy;
 
import com.tgb.service.HelloWorld;
import com.tgb.service.impl.HelloWorldImpl;
 
public class ClientMain {
 
	public static void main(String[] args){
		HelloWorldImpl factory=new HelloWorldImpl();
		//此处返回的只是远程Web Service的代理
		HelloWorld hw=factory.getHelloWorldImplPort();
		System.out.println(hw.sayHi("hejingyuan"));
	}
}

详细步骤参考:https://blog.csdn.net/weixin_40571495/article/details/86609993

鸣谢:
参考:https://www.runoob.com/webservices/webservices-tutorial.html
原文:https://blog.csdn.net/qq_18860653/article/details/53758555
原文:https://blog.csdn.net/qq_35124535/article/details/62226585
原文:https://www.cnblogs.com/yxjdragon/p/6030740.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值