WebService中的WSDL详细解析

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

WebService中的WSDL详解

一.WSDL的基本概念

二.WSDL的基本结构详解

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

  
    <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="0" name="return" 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> 

(一) definitions元素

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

(二)types元素

       WSDL采用了W3C XML模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型(不太明白: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="0" name="return" nillable="true" type="xsd:string" />  
   xsd:sequence>  
   xsd:complexType>  
   xsd:element>  
   xsd:schema>  
   wsdl:types> 

(三)import元素

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

(四)message元素

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

<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> 

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

1.sayHelloRequest:

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

2.sayHelloResponse:

(五)portType元素

 <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> 

(六)binding

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

 <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> 

(七)service元素和port元素

       service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的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> 

t2

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

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

(0)
上一篇 2026年3月16日 下午4:48
下一篇 2026年3月16日 下午4:48


相关推荐

发表回复

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

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