我的上一篇blog里提到,"系列后面的这几篇(WSDL解析跟SAAJ的使用)可当会有点理论化了,初看的话可能会感觉有点晦涩...",确实是这样的,不过,我想前面的系列文章因为是针对项目的总结,所以不可能对所汲及到的技术讨论的面面俱到.所以,在这篇blog里,基于之前在系列里讨论的WSDL4J跟Castor XML Schema(这个在前面没有怎么提到)写一个简单的Web服务解析工具,提供解析Web服务的功能,能够解析大部分与XML Schema规范兼容的Web服务,主要的目的是用来学习WSDL4J,Castor这两个工具及如何使用它们解析一个Web服务.运行.这个小实例必须:
1下载例子原代码及示例Web服务.
大家可以到我的资源去下载本程序
).
对将要解析的Web服务的有关说明:
首先,本小程序的输入数据,即将要被解析的Web 服务,可以是网络上任何可访问的Web
服务,但有个前提是,Web服务所对应的WSDL 文档中的schema定义应该符合W3C,因为我也试过很多外国网站那些Web服务,有些不可以正确的解析,原因就是那些WSDL文档的schema有些写的并不规范,所以解析会发生异常.
为了使分析问题更加简单,现在使用第1)种方法的Web服务.
2.运行程序:
如上图所示,WSCient是一个带有main函数的类,在Eclipse下运行它,就可以在控制台下查看到如下截图的一系列结果.
3.待测试的wsdl文档如下所示:
<?xmlversion="1.0"encoding="UTF-8"?>
<wsdl:definitionsxmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"xmlns:tns="http://weather.cactus.org"xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"targetNamespace="http://weather.cactus.org">
<wsdl:types>
<xsd:schematargetNamespace="http://weather.cactus.org"elementFormDefault="qualified"attributeFormDefault="qualified">
<xsd:elementname="getAllWeatherInfo">
<xsd:complexType/>
</xsd:element>
<xsd:complexTypename="ArrayOfWeather">
<xsd:sequence>
<xsd:elementname="Weather"type="tns:Weather"nillable="true"minOccurs="0"maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexTypename="Weather">
<xsd:sequence>
<xsd:elementname="ambientAirQuality"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="city"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="highestTemperature"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="highestWindPower"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="lowestTemperature"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="lowestWindPower"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="state"type="xsd:string"minOccurs="0"nillable="true"/>
<xsd:elementname="windDirection"type="xsd:string"minOccurs="0"nillable="true"/>
</xsd:sequence>
</xsd:complexType>
<xsd:elementname="getAllWeatherInfoResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:elementname="out"type="tns:ArrayOfWeather"nillable="true"minOccurs="1"maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:elementname="getWeatherInfomationByCity">
<xsd:complexType>
<xsd:sequence>
<xsd:elementname="in0"type="xsd:string"nillable="true"minOccurs="1"maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:elementname="getWeatherInfomationByCityResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:elementname="out"type="tns:Weather"nillable="true"minOccurs="1"maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:messagename="getAllWeatherInfoResponse">
<wsdl:partelement="tns:getAllWeatherInfoResponse"name="parameters"/>
</wsdl:message>
<wsdl:messagename="getAllWeatherInfoRequest">
<wsdl:partelement="tns:getAllWeatherInfo"name="parameters"/>
</wsdl:message>
<wsdl:messagename="getWeatherInfomationByCityResponse">
<wsdl:partelement="tns:getWeatherInfomationByCityResponse"name="parameters"/>
</wsdl:message>
<wsdl:messagename="getWeatherInfomationByCityRequest">
<wsdl:partelement="tns:getWeatherInfomationByCity"name="parameters"/>
</wsdl:message>
<wsdl:portTypename="WeatherServicePortType">
<wsdl:operationname="getAllWeatherInfo">
<wsdl:inputmessage="tns:getAllWeatherInfoRequest"name="getAllWeatherInfoRequest"/>
<wsdl:outputmessage="tns:getAllWeatherInfoResponse"name="getAllWeatherInfoResponse"/>
</wsdl:operation>
<wsdl:operationname="getWeatherInfomationByCity">
<wsdl:inputmessage="tns:getWeatherInfomationByCityRequest"name="getWeatherInfomationByCityRequest"/>
<wsdl:outputmessage="tns:getWeatherInfomationByCityResponse"name="getWeatherInfomationByCityResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:bindingname="WeatherServiceHttpBinding"type="tns:WeatherServicePortType">
<wsdlsoap:bindingstyle="document"transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operationname="getAllWeatherInfo">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="getAllWeatherInfoRequest">
<wsdlsoap:bodyuse="literal"/>
</wsdl:input>
<wsdl:outputname="getAllWeatherInfoResponse">
<wsdlsoap:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="getWeatherInfomationByCity">
<wsdlsoap:operationsoapAction=""/>
<wsdl:inputname="getWeatherInfomationByCityRequest">
<wsdlsoap:bodyuse="literal"/>
</wsdl:input>
<wsdl:outputname="getWeatherInfomationByCityResponse">
<wsdlsoap:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:servicename="WeatherService">
<wsdl:portbinding="tns:WeatherServiceHttpBinding"name="WeatherServiceHttpPort">
<wsdlsoap:addresslocation="http://202.118.22.171:8080/weather/services/WeatherService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这个天气服务有两个操作:
1.获取当前所有城市的天气信息。(getAllWeatherInfo())返回的是天气对象(以参数的形式返回)
2.获取某个城市的天气信息。(getWeatherInfomationByCity(String city))返回的是天气对象(以参数的形式返回)
具体地过程就不在这里分析,大家可以对照着代码里面的注释跟打印信息进行分析.
我把程序运行后的贴图,贴出来:
![自己动手编写Web服务工具[解析] 自己动手编写Web服务工具[解析]](/default/index/img?u=L2RlZmF1bHQvaW5kZXgvaW1nP3U9YUhSMGNITTZMeTl3YVdGdWMyaGxiaTVqYjIwdmFXMWhaMlZ6THpJeE5TODRNMlEwTkRaaE5HUTJabU0xT1RZelpqQmhNall6WWpCaE1USmlZbU00Tnk1d2JtYz0=)
图1接收WSDL文档并打印Schema信息
图2,构建完WSDL的castor Schema
图3 开始构建Web服务对象(1)
图4 构建Web服务对象(2)
图5 构建Web服务对象(3)
图6 构建Web服务对象(4)
图7 成功构建完Web服务对象
图8 把服务所有操作及参数都列出了