Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

WSDL文件简介

来源: jessen163 分享至:
本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
什么是WSDL?
●         WSDL代表Web Services Description Language
●         WSDL是用XML写的
●         WSDL是一个XML文档
●         WSDL用于描述Web Services
●         WSDL也用于定位Web services
●         WSDL还不是一个W3C标准
WSDL描述Web Services
WSDL代表Web Services Description Language。
WSDL是一个用XML写的XML文档。该文档描述了一个Web Service。它制定了该Web Service的位置和所提供的操作operations (或方法 methods)。

WSDL文档的结构

WSDL文档描述一个Web Service主要通过以下元素:
Element--Defines
<portType>web service执行的操作
<message>web service使用的消息
<types>web service使用的数据类型
<binding>web service使用的通信协议



WSDL文档的主要结构为

<definitions> <types> definitions of types...... </types> <message> definitions of a message... </message> <portType> definitions of a port... </portType> <binding> definitions of a binding... </binding> </definations>


WSDL Ports

<portType>元素是最重要的WSDL元素
它描述了一个web service所能够执行的操作,和所包含的消息。
<portType>元素就相当于传统编程语言中的function library函数库(或者一个模块、一个类)

WSDL Messages
<message>元素定义了一个操作的数据元素。
每一条消息包括一个或者多个部分。这些部分相当于传统编程语言中函数的参数。

WSDL Types

<types>元素定义web service所使用的数据类型
为了保证最大的平台无关性,WSDL使用XML Schema语义来定义数据类型

WSDL Bindings

<binding>元素定义为每一个port定义消息格式(message format)和协议(protocol)


下面是一些常见的命名空间:
prefix         namespace URI
wsdl           http://schemas.xmlsoap.org/wsdl/
soap          http://schemas.xmlsoap.org/wsdl/soap/
http           http://schemas.xmlsoap.org/wsdl/http/
mime          http://schemas.xmlsoap.org/wsdl/mime/
soapenc     http://schemas.xmlsoap.org/soap/encoding/
soapenv     http://schemas.xmlsoap.org/soap/envelope/
xsi            http://www.w3.org/2000/10/XMLSchema-instance
xsd            http://www.w3.org/2000/10/XMLSchema
tns            (various)

下面举例说明如何编写WSDL文档:我们做一个非常简单的加法运算服务:客户端传入SOAP请求消息,包含两个加数,然后在服务器端获取这两个加数,求和,然后返回给客户端。请求消息和响应消息结构分别如下(有效负载部分,外层的SOAP封装AXIS2会自动添加上去):

request: <SumRequest> <First>15</First> <Second>16</Second> </SumRequest> response: <SumResponse> <Result>31</Result> </SumResponse>



1.首先需要进行定义的就是soap消息的数据类型,无疑,这里需要定义两个复杂的数据类型:SumRequestSumResponse它们分别包含有子元素First、Second和Result.另外还需要定义一种异常,这里我们定义成为SumFault,比如传入的两个加数的和超出了xsd:int的范围时,抛出该异常。(注意,很多代码生成器都会根据WSDL生成代码,将SumFault部分生成为后缀为Exception的异常类。)
2.定义数据类型的目的是为传入传出消息做准备的,传入传出消息的定义方式使用message元素来定义。我们定义三种消息:SumRequest,SumResponse以及SumFault,分别为请求消息,响应消息以及出错时的消息。
3.定义好了传入传出消息后,就要定义一个portType,该节点类型定义了主要的业务操作。4.接着将定义SOAP绑定:SumSoapBinding:为SumService端口类型所定义的操作和消息指定具体传输中所使用的消息格式和协议细节。绑定的方式为SOAP,传输方式为http,消息的格式为document。
5.定义Web服务:Web服务名为SumService。
6.本Web服务的操作为Sum操作。
7.端口为:为SumSoapBing绑定指定一个地址来定义服务访问点。


根据以上七个部分编写完成的WSDL文件如下:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.zzl.org/Sum" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zzl.org/Sum"> <wsdl:documentation> The WSDL file of SumService. </wsdl:documentation> <wsdl:types> <wsdl:documentation> Data types that are used for request and response messages. </wsdl:documentation> <xsd:schema targetNamespace="http://www.zzl.org/Sum"> <xsd:element name="SumRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:int" /> <xsd:element name="Second" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SumResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="Result" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SumFault"> <xsd:complexType> <xsd:sequence> <xsd:element name="Code" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="SumRequest"> <wsdl:documentation> The data that will be transmitted to the service. </wsdl:documentation> <wsdl:part element="tns:SumRequest" name="request" /> </wsdl:message> <wsdl:message name="SumResponse"> <wsdl:documentation> The data that will be returned to the client. </wsdl:documentation> <wsdl:part element="tns:SumResponse" name="response" /> </wsdl:message> <wsdl:message name="SumFault"> <wsdl:documentation> The fault that will be thrown when fault occurs. </wsdl:documentation> <wsdl:part name="axisFault" element="tns:SumFault" /> </wsdl:message> <wsdl:portType name="SumService"> <wsdl:documentation> The SumService contains the business operation. </wsdl:documentation> <wsdl:operation name="RevokeCert"> <wsdl:documentation> The operation that do the business work. </wsdl:documentation> <wsdl:input message="tns:SumRequest" /> <wsdl:output message="tns:SumResponse" /> <wsdl:fault name="fault" message="tns:SumFault" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SumSoapBinding" type="tns:SumService"> <wsdl:documentation> The SumSoapBinding defines the SOAP message format and protocol details for Sum operation and messages defined by a RevokeService portType. </wsdl:documentation> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Sum"> <soap:operation soapAction="urn:Sum" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> <wsdl:fault name="fault"> <soap:fault use="literal" name="fault" /> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="SumService"> <wsdl:documentation> SumService provides the service of summing. </wsdl:documentation> <wsdl:port binding="tns:SumSoapBinding" name="SumSoapBinding"> <wsdl:documentation> The port defines the endpoint by specifying a soap address for SumSoapBinding. </wsdl:documentation> <soap:address location="http://www.zzl.org/ExampleService/services/SumService" /> </wsdl:port> </wsdl:service> </wsdl:definitions>


服务器端和客户端的代码是根据WSDL文件编写出来的,这才是正确的过程,但是,在国内的软件开发过程,常常是先写代码,再根据代码生成WSDL文件,这是不正确的。


1.  建立工程ExampleService
2.     编写服务器端代码:
SumService.java package org.zzl.service; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; /** * The Web Service class SumService which implement add two number and return * the result. * * @author zhangzhongl@gmail.com * @version 0.7 */ public class SumService { /** * The request soap message object. */ private OMElement requestSoap = null; /** * First addend. */ private static final String FIRST = "First"; /** * Second addend. */ private static final String SECOND = "Second"; /** * Sum Response element. */ private static final String SUM_RESPONSE = "SumResponse"; /** * Result element. */ private static final String SUM = "Result"; public OMElement Sum(OMElement soap) throws AxisFault { requestSoap = soap; OMElement first= requestSoap.getFirstChildWithName(new QName(FIRST)); OMElement second = requestSoap.getFirstChildWithName(new QName(SECOND)); int sum = Integer.parseInt(first.getText()) + Integer.parseInt(second.getText()); return getResponse(sum); } /** * Get the SOAP response message. * * @param sum * The adding result. * @return The SOAP response message. */ private OMElement getResponse(int sum) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ōmNs = factory.createOMNamespace("", ""); OMElement response = factory.createOMElement(SUM_RESPONSE, omNs); OMElement sumElement = factory.createOMElement(SUM, omNs); sumElement.setText(String.valueOf(sum)); response.addChild(sumElement); return response; } } 编写客户端代码: TestSumService.java package org.zzl.service.test; import java.io.FileInputStream;import java.io.FileNotFoundException; import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TesSumService{ private static EndpointReference targetEPR = new EndpointReference( "http://localhost/axis2/services/SumService"); public static void main(String[] args) throws FileNotFoundException, FactoryConfigurationError, XMLStreamException {OMElement requestSoapMessage = getSoapRequestMessage("data/request.xml"); Options options = new Options(); options.setAction("urn:Sum"); options.setTo(targetEPR); ServiceClient sender = null; try { sender = new ServiceClient(); sender.setOptions(options); System.out.println(sender.sendReceive(requestSoapMessage) .toStringWithConsume()); } catch (AxisFault e) { System.out.println(e.getMessage()); } } public static OMElement getSoapRequestMessage(String filePath) throws FileNotFoundException, XMLStreamException, FactoryConfigurationError { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new FileInputStream(filePath)); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement requestMessage = builder.getDocumentElement(); return requestMessage; } }



根据省的id得到省的所有电视台的id
http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVstationDataSet?theAreaID=13 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史