相关历史文章(阅读本文之前,您可能需要先看下之前的系列?)
WebService SOAP概述 - 第275篇
WSDL是什么“Lese” - 第276篇
Spring boot webservice怎么玩? - 第277篇
Spring boot cxf构建webservice服务 - 第278篇
Spring boot cxf调用webservice服务 - 第279篇
一、前言
在前面文章中,对于WebService、SOAP的一些概念进行了讲解,也提出了WSDL的概念。在调用WSDL文档的时候,那么首先要先会解读这个文档,才知道文档提供了什么方法,需要什么参数,返回值是什么。
二、WSDL是什么货色
以下是一个通过CXF生成的xsd文件:
<wsdl:definitions
xmlns:xsd="http:///2001/XMLSchema"
xmlns:wsdl="/wsdl/"
xmlns:tns=""
xmlns:soap="/wsdl/soap/"
xmlns:ns1="/soap/http" name="helloService"
targetNamespace="">
<wsdl:types>
<xs:schema xmlns:xs="http:///2001/XMLSchema"
xmlns:tns="" elementFormDefault="unqualified"
targetNamespace="" version="1.0">
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse"
type="tns:sayHelloResponse" />
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="userName"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloService">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse"
name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="helloServiceSoapBinding"
type="tns:HelloService">
<soap:binding style="document"
transport="/soap/http" />
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="helloService">
<wsdl:port binding="tns:helloServiceSoapBinding"
name="CxfServicesImplPort">
<soap:address
location="http://127.0.0.1:8080/cxf/cxfServices" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
2.1 WSDL文档组成部分
一个WSDL文档由以下几部分组成:
(1)types
types指定了WebService用到的所有数据类型:
<wsdl:types>
<xs:schema xmlns:xs="http:///2001/XMLSchema"
xmlns:tns="" elementFormDefault="unqualified"
targetNamespace="" version="1.0">
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse"
type="tns:sayHelloResponse" />
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="userName"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
上面用到了一种数据类型String
(2)message
指明一个操作所用到的数据类型:
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
sayHello(Request)是指sayHello的输入操作用到的数据类型,sayHelloResponse是指HelloWorld的输出操作用到的数据类型。二者的element元素指出了与types中对应到的具体类型。
(3)portType
指出了这个WebService所有支持的操作,就是说有哪些方法可供调用。
<wsdl:portType name="HelloService">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse"
name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
这里支持一个HelloService的sayHello调用,它的输入和输出对应到sayHello和sayHelloResponse这个两个数据类型。
(4)portTypebinding
binding元素的transport指明传输协议,这里是http协议。
operation 指明要暴露给外界调用的操作。
use属性指定输入输出的编码方式,绑定编码方式(Encoded或者Literal)序列化参数。
<wsdl:binding name="helloServiceSoapBinding"
type="tns:HelloService">
<soap:binding style="document"
transport="/soap/http" />
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
(5)service
指定服务的一些信息,主要是指定服务的访问路径。
<wsdl:service name="helloService">
<wsdl:port binding="tns:helloServiceSoapBinding"
name="CxfServicesImplPort">
<soap:address
location="http://127.0.0.1:8080/cxf/cxfServices" />
</wsdl:port>
</wsdl:service>
soap:address这里的地址就是127.0.0.1
三、通过一个WSDL怎么进行解读呐?
我们通过一个实际例子 对外开放的查询手机号归属地的一个接口文档 说明下,
该WSDL文档地址:
/WebServices/?wsdl
访问WSDL文档地址,可以得到如下的格式:
<wsdl:definitions
xmlns:soap="/wsdl/soap/"
xmlns:tm="/wsdl/mime/textMatching/"
xmlns:soapenc="/soap/encoding/"
xmlns:mime="/wsdl/mime/"
xmlns:tns="/"
xmlns:s="http:///2001/XMLSchema"
xmlns:soap12="/wsdl/soap12/"
xmlns:http="/wsdl/http/"
xmlns:wsdl="/wsdl/"
targetNamespace="/">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<a href="/" target="_blank"></a>
<strong>国内手机号码归属地查询WEB服务</strong>
,提供最新的国内手机号码段归属地数据,每月更新。
<br />
使用本站 WEB 服务请注明或链接本站:
<a href="/" target="_blank">/</a>
感谢大家的支持!
<br />
</wsdl:documentation>
<wsdl:types>
<s:schema elementFormDefault="qualified"
targetNamespace="/">
<s:element name="getMobileCodeInfo">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="mobileCode"
type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="userID"
type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getMobileCodeInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1"
name="getMobileCodeInfoResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getDatabaseInfo">
<s:complexType />
</s:element>
<s:element name="getDatabaseInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1"
name="getDatabaseInfoResult" type="tns:ArrayOfString" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfString">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded"
name="string" nillable="true" type="s:string" />
</s:sequence>
</s:complexType>
<s:element name="string" nillable="true" type="s:string" />
<s:element name="ArrayOfString" nillable="true"
type="tns:ArrayOfString" />
</s:schema>
</wsdl:types>
<wsdl:message name="getMobileCodeInfoSoapIn">
<wsdl:part name="parameters" element="tns:getMobileCodeInfo" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoSoapOut">
<wsdl:part name="parameters"
element="tns:getMobileCodeInfoResponse" />
</wsdl:message>
<wsdl:message name="getDatabaseInfoSoapIn">
<wsdl:part name="parameters" element="tns:getDatabaseInfo" />
</wsdl:message>
<wsdl:message name="getDatabaseInfoSoapOut">
<wsdl:part name="parameters"
element="tns:getDatabaseInfoResponse" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetIn">
<wsdl:part name="mobileCode" type="s:string" />
<wsdl:part name="userID" type="s:string" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>
<wsdl:message name="getDatabaseInfoHttpGetIn" />
<wsdl:message name="getDatabaseInfoHttpGetOut">
<wsdl:part name="Body" element="tns:ArrayOfString" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostIn">
<wsdl:part name="mobileCode" type="s:string" />
<wsdl:part name="userID" type="s:string" />
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>
<wsdl:message name="getDatabaseInfoHttpPostIn" />
<wsdl:message name="getDatabaseInfoHttpPostOut">
<wsdl:part name="Body" element="tns:ArrayOfString" />
</wsdl:message>
<wsdl:portType name="MobileCodeWSSoap">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3>
<p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID)
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoSoapIn" />
<wsdl:output message="tns:getMobileCodeInfoSoapOut" />
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地数据库信息</h3>
<p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoSoapIn" />
<wsdl:output message="tns:getDatabaseInfoSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpGet">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3>
<p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID)
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoHttpGetIn" />
<wsdl:output message="tns:getMobileCodeInfoHttpGetOut" />
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地数据库信息</h3>
<p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoHttpGetIn" />
<wsdl:output message="tns:getDatabaseInfoHttpGetOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpPost">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3>
<p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID)
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoHttpPostIn" />
<wsdl:output message="tns:getMobileCodeInfoHttpPostOut" />
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<br />
<h3>获得国内手机号码归属地数据库信息</h3>
<p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoHttpPostIn" />
<wsdl:output message="tns:getDatabaseInfoHttpPostOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MobileCodeWSSoap"
type="tns:MobileCodeWSSoap">
<soap:binding
transport="/soap/http" />
<wsdl:operation name="getMobileCodeInfo">
<soap:operation
soapAction="/getMobileCodeInfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<soap:operation
soapAction="/getDatabaseInfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSSoap12"
type="tns:MobileCodeWSSoap">
<soap12:binding
transport="/soap/http" />
<wsdl:operation name="getMobileCodeInfo">
<soap12:operation
soapAction="/getMobileCodeInfo" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<soap12:operation
soapAction="/getDatabaseInfo" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSHttpGet"
type="tns:MobileCodeWSHttpGet">
<http:binding verb="GET" />
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo" />
<wsdl:input>
<http:urlEncoded />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<http:operation location="/getDatabaseInfo" />
<wsdl:input>
<http:urlEncoded />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSHttpPost"
type="tns:MobileCodeWSHttpPost">
<http:binding verb="POST" />
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo" />
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded" />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<http:operation location="/getDatabaseInfo" />
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded" />
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MobileCodeWS">
<wsdl:documentation
xmlns:wsdl="/wsdl/">
<a href="/" target="_blank"></a>
<strong>国内手机号码归属地查询WEB服务</strong>
,提供最新的国内手机号码段归属地数据,每月更新。
<br />
使用本站 WEB 服务请注明或链接本站:
<a href="/" target="_blank">/</a>
感谢大家的支持!
<br />
</wsdl:documentation>
<wsdl:port name="MobileCodeWSSoap"
binding="tns:MobileCodeWSSoap">
<soap:address
location="/WebServices/" />
</wsdl:port>
<wsdl:port name="MobileCodeWSSoap12"
binding="tns:MobileCodeWSSoap12">
<soap12:address
location="/WebServices/" />
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpGet"
binding="tns:MobileCodeWSHttpGet">
<http:address
location="/WebServices/" />
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpPost"
binding="tns:MobileCodeWSHttpPost">
<http:address
location="/WebServices/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
可以自己用浏览器打开访问一下;
一眼望去,可能接口看起来很复杂,较为繁琐。 但是作为开发人员,使用的时候, 我们可以查看关注点信息,就能实现接口的调用了。
那么我们需要了解哪些信息呢?
从上面的文档里面,我们需要从头部(最上面一行)了解到:
targetNamespace="/"
通过wsdl:portType可以该文档提供了如下方法:
getMobileCodeInfo:获得国内手机号码归属地省份、地区和手机卡类型信息。
getDatabaseInfo:获得国内手机号码归属地数据库信息。
通过types就能找到getMobileCodeInfo需要传递的参数:
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="mobileCode"
type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="userID"
type="s:string" />
</s:sequence>
需要两个参数:
mobileCode:手机号码,最少前7位数字
userID:商业用户ID) 免费用户为空字符串
通过service找到请求地址:/WebServices/
综上分析就可以得出,发起SOAP请求所需要的数据了:
WebService的URL:/WebServices/
Namespace:/
method:getMobileCodeInfo/getDatabaseInfo
params:mobileCode(必须)、userId(非必须)
三、常用的免费WebService接口
(1)天气预报Web服务,数据来源于中国气象局
/WebServices/?wsdl
(2)IP地址来源搜索 WEB 服务
/WebServices/?wsdl
(3)随机英文、数字和中文简体字 WEB 服务
/WebServices/?wsdl
(4)中国邮政编码 <-> 地址信息双向查询/搜索 WEB 服务
/WebServices/?wsdl
(5)验证码图片 WEB 服务 支持中文、字母、数字 图像和多媒体
/WebServices/?wsdl
对于WSDL的介绍,就到这里了,那我们自己怎么发布一个soap webservice呢?欲知详情,下回分解。
我就是我,是颜色不一样的烟火。
我就是我,是与众不同的小苹果。
à悟空学院:/Rg3fKJD
学院中有Spring Boot相关的课程!
SpringBoot视频: /R3QepWG
Spring Cloud视频: /R3QeRZc
SpringBoot Shiro视频: /R3QDMbh
SpringBoot交流平台: /R3QDhU0
SpringData和JPA视频: /R1pSojf
SpringSecurity5.0视频: /EwlLjHh
Sharding-JDBC分库分表实战: /E4lpD6e