wsdl自学笔记

本文详细介绍了WSDL(Web Services Description Language),包括其概述、文档结构、端口、消息、类型等关键概念,并通过实例展示了如何定义和使用WSDL。深入探讨了WSDL在Web服务开发中的应用,帮助开发者更好地理解和使用这一标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:http://blog.youkuaiyun.com/yangdongxin/article/details/5071744  自学记录的笔记

 

 

1、WSDL概述

WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问。

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

WSDL 指网络服务描述语言 (Web Services Description Language)。

WSDL 是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作(或方法)。

 

1.1 WSDL 文档结构

WSDL 文档是利用这些主要的元素来描述某个 web service 的:

  元素                             定义

 

<message>                      web service 使用的消息

 

<portType>                      web service 执行的操作

 

<types>                         web service 使用的数据类型

 

<binding>                       web service 使用的通信协议

 

 

 

一个 WSDL 文档的主要结构是类似这样的:

<definitions>

    <types>definition of types........</types>

    <message>definition of a message....</message>

    <portType>definition of a port.......</portType>

    <binding>definition of a binding....</binding>

</definitions>

 

WSDL 文档可包含其它的元素,比如 extension 元素,以及一个 service 元素,此元素可把若干个 web services 的定义组合在一个单一的 WSDL 文档中。

 

1.1.1 WSDL Bindings

<binding> 元素为每个端口定义消息格式和协议细节。

就是最初定义每个操作的格式和协议细节等。

 

<message name="say">

       <part name="parameters" element="tns:say" />

    </message>

    <message name="sayResponse">

       <part name="parameters" element="tns:sayResponse" />

    </message>

    <portType name="Hello">

       <operation name="say">

           <input message="tns:say" />

           <output message="tns:sayResponse" />

       </operation>

    </portType>

    <binding name="HelloPortBinding" type="tns:Hello">

       <soap:binding transport="http://schemas.xmlsoap.org/soap/http"

           style="document" />

       <operation name="say">

           <soap:operation soapAction="" />

           <input>

               <soap:body use="literal" />

           </input>

           <output>

              <soap:body use="literal" />

           </output>

       </operation>

    </binding>

 

binding 元素有两个属性 - name 属性和 type 属性。

name 属性定义 binding 的名称,而 type 属性指向用于 binding 的端口,在这个例子中是 " Hello " 端口。

soap:binding 元素有两个属性 - style 属性和 transport 属性。

style 属性可取值 "rpc" 或 "document"。在这个例子中我们使用 document。transport 属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP。

operation 元素定义了每个端口提供的操作符。

对于每个操作,相应的 SOAP 行为都需要被定义。同时您必须如何对输入和输出进行编码。在这个例子中我们使用了 "literal"。

 

 

 

1.1.2  WSDL 端口

<portType>元素是最重要的 WSDL 元素。

它可描述一个 web service、可被执行的操作,以及相关的消息。

可以把 <portType> 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。

就是定义该web service具有的操作方法。

反向生成服务端就是实现该接口,然后实现方法进行发布。

生成客户端就是新建个service,然后获取port,该port拥有发布的方法,然后调用方法。

 

One-Way 操作: 此操作可接受消息,但不会返回响应。(即有输入,没有输出)

 

Request-response: 此操走可接受一个请求并会返回一个响应。(有输入又有输出)

 

 

1.1.3  WSDL 消息

<message> 元素定义一个操作的数据元素。

每个消息均由一个或多个部件组成。可以把这些部件比作传统编程语言中一个函数调用的参数。

 

就是该web service所具有的方法的一一解析,是对<portType>里定义的方法一一进行解析说明。

 

 

 

1.1.4  WSDL types

 

<types> 元素定义 web service 使用的数据类型。

为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。

有些wsdl的数据类型是内嵌进wsdl里定义的,而有些wsdl是单独分开的,是存放在xsd文件中的。例如:

<types>

    <xsd:schema>

       <xsd:import namespace="http://ws/"

           schemaLocation="http://localhost:8080/HelloService?xsd=1">

       </xsd:import>

    </xsd:schema>

</types>

 

数据类型是存放在http://localhost:8080/HelloService?xsd=1这里额外定义的

 

 

所以我们一般看WSDL是从下往上看的。

 

1.2 WSDL 实例

这是某个 WSDL 文档的简化的片段:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. -->

<definitions targetNamespace="http://ws/" name="HelloService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

  <types>

    <xsd:schema>

      <xsd:import namespace="http://ws/" schemaLocation="HelloService_schema1.xsd"/>

    </xsd:schema>

  </types>

  <message name="sayHello">

    <part name="parameters" element="tns:sayHello"/>

  </message>

  <message name="sayHelloResponse">

    <part name="parameters" element="tns:sayHelloResponse"/>

  </message>

  <portType name="Hello">

    <operation name="sayHello">

      <input message="tns:sayHello"/>

      <output message="tns:sayHelloResponse"/>

    </operation>

  </portType>

  <binding name="HelloPortBinding" type="tns:Hello">

    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>

    <operation name="sayHello">

      <soap:operation soapAction=""/>

      <input>

        <soap:body use="literal"/>

      </input>

      <output>

        <soap:body use="literal"/>

      </output>

    </operation>

  </binding>

  <service name="HelloService">

    <port name="HelloPort" binding="tns:HelloPortBinding">

      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>

    </port>

  </service>

</definitions>

 

数据类型文件HelloService_schema1.xsd如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" targetNamespace="http://ws/" xmlns:tns="http://ws/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="sayHello" type="tns:sayHello"/>

  <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

  <xs:complexType name="sayHello">

    <xs:sequence>

      <xs:element name="arg0" type="xs:string" minOccurs="0"/>

      <xs:element name="arg1" type="xs:int"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="sayHelloResponse">

    <xs:sequence>

      <xs:element name="return" type="xs:string" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:schema>

 

 

在这个例子中,<portType> 元素把 " Hello " 定义为某个端口的名称,把 " sayHello " 定义为某个操作的名称。

操作 " sayHello " 的数据类型在HelloService_schema1.xsd额外定义,拥有两个输入消息,分别名为 " arg0",” arg1”,以及一个名为 " sayHelloResponse " 的输出消息。

<message> 元素可定义每个消息的部件,以及相关联的数据类型。

对比传统的编程,Hello是一个函数库,而 " sayHello " 是带有输入参数 " arg0" , “arg1”和返回参数 sayHelloResponse的一个函数。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值