WSDL文件简介

[size=medium]本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
[b]什么是WSDL?[/b]
● WSDL代表Web Services Description Language
● WSDL是用XML写的
● WSDL是一个XML文档
● WSDL用于描述Web Services
● WSDL也用于定位Web services
● WSDL还不是一个W3C标准
[b]WSDL描述Web Services[/b]
WSDL代表Web Services Description Language。
WSDL是一个用XML写的XML文档。该文档描述了一个Web Service。它制定了该Web Service的位置和所提供的操作operations (或方法 methods)。
[b]
WSDL文档的结构[/b]
WSDL文档描述一个Web Service主要通过以下元素:
Element--Defines
[b][color=indigo]web service执行的操作
web service使用的消息
web service使用的数据类型
web service使用的通信协议[/color][/b]


WSDL文档的主要结构为[/size]




definitions of types......


definitions of a message...


definitions of a port...


definitions of a binding...



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

[b]WSDL Messages[/b]
元素定义了一个操作的数据元素。
每一条消息包括一个或者多个部分。这些部分相当于传统编程语言中函数的参数。
[b]
WSDL Types[/b]
元素定义web service所使用的数据类型
为了保证最大的平台无关性,WSDL使用XML Schema语义来定义数据类型
[b]
WSDL Bindings[/b]
元素定义为每一个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会自动添加上去):
[/size]

request:

15
16


response:

31




[size=medium]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文件如下:[/size]


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

The WSDL file of SumService.



Data types that are used for request and response messages.




























The data that will be transmitted to the service.





The data that will be returned to the client.






The fault that will be thrown when fault occurs.





The SumService contains the business operation.



The operation that do the business work.








The SumSoapBinding defines the SOAP message format and
protocol details for Sum operation and messages defined by a
RevokeService portType.

transport="http://schemas.xmlsoap.org/soap/http" />















SumService provides the service of summing.



The port defines the endpoint by specifying a soap
address for SumSoapBinding.






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


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 [email protected]
* @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;
}
}



[color=red]根据省的id得到省的所有电视台的id[/color]

http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVstationDataSet?theAreaID=13

http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

你可能感兴趣的:(一点一滴)