分享

WSDL文件简介(附例子)

 WindySky 2011-07-12
本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
 首先明确的一点是WSDL现在有两个版本,分别为WSDL 1.1和WSDL 2.0,W3C的官方文档地址分别为:
Web Services Descrīption Language (WSDL) 1.1
W3C Note 15 March 2001
Web Services Descrīption Language (WSDL) Version 2.0 Part 0: Primer
W3C Working Draft 26 March 2007
 
其中很多应用还是以版本1.1为基础实现。下面是2.0与1.1的区别:
Adding further semantics to the descrīption language. 
Removal of message constructs.
No support for operator overloading.
PortTypes renamed to interfaces.
Ports renamed to endpoints.
 
下面是一些常见的命名空间:
prefix         namespace URI
wsdl           http://schemas./wsdl/
soap          http://schemas./wsdl/soap/
http           http://schemas./wsdl/http/
mime          http://schemas./wsdl/mime/
soapenc     http://schemas./soap/encoding/
soapenv     http://schemas./soap/envelope/
xsi            http://www./2000/10/XMLSchema-instance
xsd            http://www./2000/10/XMLSchema
tns            (various)
 
对于WSDL规范,可以参考以上两个官方文档,本文主要介绍如何编写WSDL文档(其实官方文档中已经给出了很多例子和方法,这里只是简单的翻译与重复介绍)。
 
下面举例说明如何编写WSDL文档:
我们做一个非常简单的加法运算服务:客户端传入SOAP请求消息,包含两个加数,然后在服务器端获取这两个加数,求和,然后返回给客户端。
请求消息和响应消息结构分别如下(有效负载部分,外层的SOAP封装AXIS2会自动添加上去):
request:
<SumRequest>
    <First>15</First>
    <Second>16</Second>
</SumRequest>
 
response:
<SumResponse>
    <Result>31</Result>
</SumResponse>
 
1.首先需要进行定义的就是soap消息的数据类型,无疑,这里需要定义两个复杂的数据类型:
SumRequest
SumResponse
它们分别包含有子元素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./wsdl/soap/"
    xmlns:tns="http://www./Sum"
    xmlns:wsdl="http://schemas./wsdl/"
    xmlns:xsd="http://www./2001/XMLSchema"
    targetNamespace="http://www./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./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./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./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 ōptions = 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)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多