WSDL、详解

WSDL、详解WebService 中的 WSDL 详解 nbsp nbsp nbsp nbsp nbsp nbsp nbsp 有人在 WebService 开发的时候 特别是和第三方有接口的时候 走的是 SOAP 协议 然后用户 或后台 给你一个 WSDL 文件 或网址 说按照上面的进行适配 这时就要对 WebService 的 WSDL 有一定的理解 本文将对 WSDL WebService 描述语言 进

WebService中的WSDL详解

一.WSDL的基本概念

二.WSDL的基本结构详解

下面通过一份wsdl文档,来详细解读WSDL结构:

 
 
  1. xml version="1.0" encoding="UTF-8" ?>
  2. <wsdl:definitions
  3. targetNamespace = "http://com.liuxiang.xfireDemo/HelloService"
  4. xmlns:tns = "http://com.liuxiang.xfireDemo/HelloService"
  5. xmlns:wsdlsoap = "http://schemas.xmlsoap.org/wsdl/soap/"
  6. xmlns:soap12 = "http://www.w3.org/2003/05/soap-envelope"
  7. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  8. xmlns:soapenc11 = "http://schemas.xmlsoap.org/soap/encoding/"
  9. xmlns:soapenc12 = "http://www.w3.org/2003/05/soap-encoding"
  10. xmlns:soap11 = "http://schemas.xmlsoap.org/soap/envelope/"
  11. xmlns:wsdl = "http://schemas.xmlsoap.org/wsdl/" >
  12. <wsdl:types>
  13. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  14. attributeFormDefault = "qualified" elementFormDefault = "qualified"
  15. targetNamespace = "http://com.liuxiang.xfireDemo/HelloService" >
  16. <xsd:element name="sayHello">
  17. <xsd:complexType>
  18. <xsd:sequence>
  19. <xsd:element maxOccurs= name="sayHelloRequest">
  20. <wsdl:part name="parameters" element="tns:sayHello" />
  21. wsdl:message>
  22. <wsdl:portType name="HelloServicePortType">
  23. <wsdl:operation name="sayHello">
  24. <wsdl:input name="sayHelloRequest"
  25. message = "tns:sayHelloRequest" />
  26. <wsdl:output name="sayHelloResponse"
  27. message = "tns:sayHelloResponse" />
  28. wsdl:operation>
  29. wsdl:portType>
  30. <wsdl:binding name="HelloServiceHttpBinding"
  31. type = "tns:HelloServicePortType" >
一:WSDL定义

WSDL是一个用于精确描述Web服务的文档,WSDL文档是一个遵循WSDL XML模式的XML文档。WSDL 文档将Web服务定义为服务访问点或端口的集合。在 WSDL 中,由于服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,因此可以对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操作的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了可以再次使用的绑定。将Web访问地址与可再次使用的绑定相关联,可以定义一个端口,而端口的集合则定义为服务。

一个WSDL文档通常包含7个重要的元素,即types、import、message、portType、operation、binding、service元素。这些元素嵌套在definitions元素中,definitions是WSDL文档的根元素。文章的下一部分将会详细介绍WSDL的基本结构。

二:WSDL的基本结构–概述
  • Types – 数据类型定义的容器,它使用某种类型系统(一般地使用XML Schema中的类型系统)。
  • Message – 通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构。
  • Operation – 对服务中所支持的操作的抽象描述,一般单个Operation描述了一个访问入口的请求/响应消息对。
  • PortType – 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。
  • Binding – 特定端口类型的具体协议和数据格式规范的绑定。
  • Port – 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。
  • Service- 相关服务访问点的集合。
三:WSDL的基本结构–详述

本节将通过一个例子详细描述WSDL文档每个元素的作用。下面一个例子是一个简单的WSDL文档的内容,该文档的产生可以参见我的另外一篇文章:xfire开发实例–HelloWorld篇 (http://blog.csdn.net/juxtapose/archive/2007/09/10/1779849.aspx)。
一个简单的Web Service的WSDL文档,该服务支持名为sayHello的唯一操作,该操作通过在http上运行SOAP协议来实现的。该请求接受一个字符串name,经过处理后返回一个简单的字符串。文档如下:

 
    <wsdl:definitions  targetNamespace="http://com.liuxiang.xfireDemo/HelloService" xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://com.liuxiang.xfireDemo/HelloService"> <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" />  
   xsd:sequence>  
   xsd:complexType>  
   xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />  
   xsd:sequence>  
   xsd:complexType>  
   xsd:element>  
   xsd:schema>  
   wsdl:types> <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="tns:sayHelloResponse" />  
   wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="tns:sayHello" />  
   wsdl:message> <wsdl:portType name="HelloServicePortType"> <wsdl:operation name="sayHello"> <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" /> <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />  
   wsdl:operation>  
   wsdl:portType> <wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction="" /> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body use="literal" />  
   wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body use="literal" />  
   wsdl:output>  
   wsdl:operation>  
   wsdl:binding> <wsdl:service name="HelloService"> <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding"> <wsdlsoap:address  location="http://localhost:8080/xfire/services/HelloService" />  
   wsdl:port>  
   wsdl:service>  
   wsdl:definitions> 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

types元素使用XML模式语言声明在WSDL文档中的其他位置使用的复杂数据类型与元素;


import元素类似于XML模式文档中的import元素,用于从其他WSDL文档中导入WSDL定义;


message元素使用在WSDL文档的type元素中定义或在import元素引用的外部WSDL文档中定义的XML模式的内置类型、复杂类型或元素描述了消息的有效负载;


portType元素和operation元素描述了Web服务的接口并定义了他的方法。portType元素和operation元素类似于java接口和接口中定义的方法声明。operation元素使用一个或者多个message类型来定义他的输入和输出的有效负载;


<wsdl:definitions  targetNamespace="http://com.liuxiang.xfireDemo/HelloService" xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">  
       wsdl:definitions> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、types元素
WSDL采用了W3C XML模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型。当声明消息部分的有效负载时,消息定义使用了在types元素中定义的数据类型和元素。在本文的WSDL文档中的types定义:

<wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://com.liuxiang.xfireDemo/HelloService"> <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" />  
       xsd:sequence>  
       xsd:complexType>  
       xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />  
       xsd:sequence>  
       xsd:complexType>  
       xsd:element>  
       xsd:schema>  
       wsdl:types> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
<wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="tns:sayHelloResponse" />  
       wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="tns:sayHello" />  
       wsdl:message> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
 <wsdl:portType name="HelloServicePortType"> <wsdl:operation name="sayHello"> <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" /> <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />  
       wsdl:operation>  
       wsdl:portType> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
<wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction="" /> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body use="literal" />  
       wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body use="literal" />  
       wsdl:output>  
       wsdl:operation>  
       wsdl:binding> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
<wsdl:service name="HelloService"> <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding"> <wsdlsoap:address  location="http://localhost:8080/xfire/services/HelloService" />  
       wsdl:port>  
       wsdl:service> 
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这部分是具体的Web服务的定义,在这个名为HelloService的Web服务中,提供了一个服务访问入口,访问地址是http://localhost:8080/xfire/services/HelloService,使用的消息模式是由前面的binding所定义的。

  ="hljs-tag"><wsdlsoap:binding style="document"
  • transport = "http://schemas.xmlsoap.org/soap/http" />
  • <wsdl:operation name="sayHello">
  • <wsdlsoap:operation soapAction="" />
  • <wsdl:input name="sayHelloRequest">
  • <wsdlsoap:body use="literal" />
  • wsdl:input>
  • <wsdl:output name="sayHelloResponse">
  • <wsdlsoap:body use="literal" />
  • wsdl:output>
  • wsdl:operation>
  • wsdl:binding>
  • <wsdl:service name="HelloService">
  • <wsdl:port name="HelloServiceHttpPort"
  • binding = "tns:HelloServiceHttpBinding" >
  • <wsdlsoap:address
  • location = "http://localhost:8080/xfire/services/HelloService" />
  • wsdl:port>
  • wsdl:service>
  • wsdl:definitions>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

      (一) definitions元素

             所有的WSDL文档的根元素均是definitions元素。该元素封装了整个文档,同时通过其name提供了一个WSDL文档。除了提供一个命名空间(targetNamespace)外,该元素没有其他作用,故不作详细描述。

      (二)types元素

             WSDL采用了W3C XML模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型(不太明白:XML模式内置类型中没有描述的各种数据类型)。当声明消息部分的有效时,消息定义使用了在types元素中定义的数据类型和元素。在本文的WSDL文档中的types定义:

       
            
      1. <wsdl:types>
      2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      3. attributeFormDefault = "qualified" elementFormDefault = "qualified"
      4. targetNamespace = "http://com.liuxiang.xfireDemo/HelloService" >
      5. <xsd:element name="sayHello">
      6. <xsd:complexType>
      7. <xsd:sequence>
      8. <xsd:element maxOccurs="1" minOccurs="1"
      9. name = "name" nillable = "true" type = "xsd:string" />
      10. xsd:sequence>
      11. xsd:complexType>
      12. xsd:element>
      13. <xsd:element name="sayHelloResponse">
      14. <xsd:complexType>
      15. <xsd:sequence>
      16. <xsd:element maxOccurs="1" minOccurs="0"
      17. name = "return" nillable = "true" type = "xsd:string" />
      18. xsd:sequence>
      19. xsd:complexType>
      20. xsd:element>
      21. xsd:schema>
      22. wsdl:types>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22

      (三)import元素

      <wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/> 
            
      • 1
      • 1

      (四)message元素

             message元素描述了Web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载;还可以描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用RPC样式还是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递:

       
            
      1. <wsdl:message name="sayHelloResponse">
      2. <wsdl:part name="parameters" element="tns:sayHelloResponse" />
      3. wsdl:message>
      4. <wsdl:message name="sayHelloRequest">
      5. <wsdl:part name="parameters" element="tns:sayHello" />
      6. wsdl:message>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

             该部分是消息格式的抽象定义:定义了两个消息sayHelloResponse和sayHelloRequest:

      1.sayHelloRequest:

      sayHello操作的请求消息格式,由一个消息片断组成,名字为parameters,元素是我们前面定义的types中的元素;

      2.sayHelloResponse:

      (五)portType元素

             portType元素定义了Web服务的抽象接口。该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来说明Web服务实现使用的Internet协议、编码方案以及Internet地址。 
      一个portType中可以定义多个operation,一个operation可以看作是一个方法,本文中WSDL文档的定义:

       
            
      1. <wsdl:portType name="HelloServicePortType">
      2. <wsdl:operation name="sayHello">
      3. <wsdl:input name="sayHelloRequest"
      4. message = "tns:sayHelloRequest" />
      5. <wsdl:output name="sayHelloResponse"
      6. message = "tns:sayHelloResponse" />
      7. wsdl:operation>
      8. wsdl:portType>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      (六)binding

             binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。通常binding元素与协议专有的元素和在一起使用,本文中的例子:

       
            
      1. <wsdl:binding name="HelloServiceHttpBinding"
      2. type = "tns:HelloServicePortType" >
      3. <wsdlsoap:binding style="document"
      4. transport = "http://schemas.xmlsoap.org/soap/http" />
      5. <wsdl:operation name="sayHello">
      6. <wsdlsoap:operation soapAction="" />
      7. <wsdl:input name="sayHelloRequest">
      8. <wsdlsoap:body use="literal" />
      9. wsdl:input>
      10. <wsdl:output name="sayHelloResponse">
      11. <wsdlsoap:body use="literal" />
      12. wsdl:output>
      13. wsdl:operation>
      14. wsdl:binding>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      (七)service元素和port元素

             service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。文档中的例子:

       
            
      1. <wsdl:service name="HelloService">
      2. <wsdl:port name="HelloServiceHttpPort"
      3. binding = "tns:HelloServiceHttpBinding" >
      4. <wsdlsoap:address
      5. location = "http://localhost:8080/xfire/services/HelloService" />
      6. wsdl:port>
      7. wsdl:service>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

      t2

    版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

    发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211604.html原文链接:https://javaforall.net

    (0)
    上一篇 2026年3月18日 下午10:02
    下一篇 2026年3月18日 下午10:03


    相关推荐

    • SpringBoot+Mybatis实现分页查询[通俗易懂]

      SpringBoot+Mybatis实现分页查询[通俗易懂]文章目录前言1.引入依赖2.Mapper中接口3.修改XML文件4.controller层调用接口5.测试总结前言分页查询是在web开发中常用的一种技术,当某个页面查询返回的数据量较大时,为了提高性能和用户体验不能将所有数据一次性返回给过前端,这时候就需要用到分页查询了PageHelper是一款开源的Mybatis第三方物理分页插件,springboot项目中集成PageHelper插件非…

      2022年5月5日
      47
    • Java Swing 图形界面开发简介

      Java Swing 图形界面开发简介JavaSwingGUI 图形界面窗口开发基础教程 本教程将系统性地详细介绍 Swing 开发中常用的一些组件 布局管理器等相关知识技术 并且每章节都将通过代码实例展示实际应用 Swing 是 Java 为图形界面应用开发提供的一组工具包 是 Java 基础类的一部分 Swing 包含了构建图形界面 GUI 的各种组件 如 窗口 标签 按钮 文本框等 Swing 提供了许多比 AWT 更好的屏幕显示元素 使用纯 Java 实现 能够更好的兼容跨平台运行 JavaSwing 图形界面开发目录

      2025年10月16日
      3
    • 利用MDK软件生成bin文件的简单方法

      利用MDK软件生成bin文件的简单方法一、缘由:之前学习KeilMDK-ARM软件,找了好久生成bin文件的方法,这次分享最简单的,所以写了此篇博文二、操作步骤:1、打开“KeilMDK-ARM软件”-找到魔术棒“Optionsfortarget…”:2、点击“User”选择AfterBulid/Rebuild状态下的“▢Run#1”:3、点击后面的空白处,写入命令,,最后关闭窗口,重新编译软件,即可生成bin文件:4、具体命令如下:命令格式1:fromelf.exe–bin-o“%L@L.

      2022年10月20日
      4
    • Python注释

      Python注释单行注释python中单行注释采用#开头[cclang='python']print‘hellopython’#thisisacomment[/cc]多行注释然后pyt

      2022年7月5日
      23
    • python 数组操作中的 “:” “:: ” “, ” python 中的 [:-1] 和 [::-1] [-1:-2:-1] [

      python 数组操作中的 “:” “:: ” “, ” python 中的 [:-1] 和 [::-1] [-1:-2:-1] [使用 python 版本 3 7 首先先了解下 python3 7 中的下标 python 下标有两套 一套是正的 一套是负的 a python 中的 python 的下标可以如下组 python 正下标 012345 负下标 6 5 4 3 2 1 对应位置的正下标 负下标 len a 使用正下标时 下标 i

      2026年3月18日
      2
    • 扣子(Coze)基础:扣子入门教程——初识扣子

      扣子(Coze)基础:扣子入门教程——初识扣子

      2026年3月12日
      2

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注

    关注全栈程序员社区公众号