天天看點

WTP1.0開發WebService之使用WSDL生成Service

1.确定安裝了Apache Tomcat(這裡使用Tomcat5.0),建立一個dynamic Web project取名為 AreaProj。

2.示例中使用的WSDL文檔。(該文檔定義了一個計算長方形面積的服務)

AreaService.wsdl

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:tns="http://tempuri.org/AreaService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AreaService"

targetNamespace="http://tempuri.org/AreaService/">

  <wsdl:types>

<xsd:schema targetNamespace=http://tempuri.org/AreaService/

 xmlns:xsd="http://www.w3.org/2001/XMLSchema">

      <xsd:element name="area" type="xsd:float"/>

      <xsd:element name="parameters" type="tns:dimensions"/>

      <xsd:complexType name="dimensions">

          <xsd:sequence>

                 <xsd:element name="width" type="xsd:float"></xsd:element>

                 <xsd:element name="height" type="xsd:float"></xsd:element>

          </xsd:sequence>

      </xsd:complexType>

    </xsd:schema>

  </wsdl:types>

  <wsdl:message name="CalculateRectAreaResponse">

    <wsdl:part element="tns:area" name="area"/>

  </wsdl:message>

  <wsdl:message name="CalculateRectAreaRequest">

    <wsdl:part element="tns:parameters" name="parameters"/>

  <wsdl:portType name="AreaService">

    <wsdl:operation name="CalculateRectArea">

      <wsdl:input message="tns:CalculateRectAreaRequest"/>

      <wsdl:output message="tns:CalculateRectAreaResponse"/>

    </wsdl:operation>

  </wsdl:portType>

  <wsdl:binding name="AreaServiceSOAP" type="tns:AreaService">

    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <soap:operation soapAction="http://tempuri.org/AreaService/NewOperation"/>

      <wsdl:input>

        <soap:body use="literal"/>

      </wsdl:input>

      <wsdl:output>

      </wsdl:output>

  </wsdl:binding>

  <wsdl:service name="AreaService">

    <wsdl:port binding="tns:AreaServiceSOAP" name="AreaServiceSOAP">

      <soap:address location="http://tempuri.org"/>

    </wsdl:port>

  </wsdl:service>

</wsdl:definitions>

3.将AreaService.wsdl放在/WebContent下,滑鼠右鍵選中該檔案,選擇New-Other-WebService,配置如下。 

4.一路Next,如果沒有啟動Tomcat,會提示Start Server。最後選擇Finish。

5.在/src下生成部分java檔案,其中一個AreaServiceSOAPImpl.java。将其内容修改為如下所示。

6.在/WebContent/wsdl/下會生成AreaServiceSOAP.wsdl。滑鼠右鍵選中該檔案,選擇New-Other-WebService Client,配置如下。

7.點選兩個Next後,到達Client環境配置。配置如下。

8.選擇Finish。出現如下畫面。

9.選擇calculateRectArea進行測試,計算長方形面積。測試結果如下。

10.檢視傳遞的SOAP。

request部分:

response部分:

本文轉自zhangjunhd51CTO部落格,原文連結:http://blog.51cto.com/zhangjunhd/21440,如需轉載請自行聯系原作者

繼續閱讀