java學習

          java學習

           

          soap的基于契約優先WSDL的開發webservice流程



          --

          開發流程:

          一、先寫schema或者wsdl文件

          1)新建一個項目作為服務端,在src目錄下簡歷文件夾META-INF/wsdl/mywsdl.wsdl文件。(2)在mywsdl.wsdl文件中編寫自己的內容,如下:

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

          <wsdl:definitions

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

            xmlns:tns="http://www.example.org/mywsdl/"

            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

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

            name="MyServiceImplService"

            targetNamespace="http://www.example.org/mywsdl/">

            <wsdl:types>

              <xsd:schema targetNamespace="http://www.example.org/mywsdl/">

              <!-- 編寫服務方法和返回值 -->

              <xsd:element name="add" type="tns:add"/>

              <xsd:element name="addResponse" type="tns:addResponse"/>

              <xsd:element name="divide" type="tns:divide"/>

              <xsd:element name="divideResponse" type="tns:divideResponse"/>

              <xsd:element name="licenseInfo" type="xsd:string"/>

             

              <xsd:complexType name="add">

                 <xsd:sequence>

                    <xsd:element name="a" type="xsd:int"/>

                    <xsd:element name="b" type="xsd:int"/>

                 </xsd:sequence>

              </xsd:complexType>

              <xsd:complexType name="addResponse">

                 <xsd:sequence>

                    <xsd:element name="addResult" type="xsd:int"/>

                 </xsd:sequence>

              </xsd:complexType>

             

              <xsd:complexType name="divide">

                 <xsd:sequence>

                    <xsd:element name="num1" type="xsd:int"/>

                    <xsd:element name="num2" type="xsd:int"/>

                 </xsd:sequence>

              </xsd:complexType>

              <xsd:complexType name="divideResponse">

                 <xsd:sequence>

                    <xsd:element name="divideResult" type="xsd:int"/>

                 </xsd:sequence>

              </xsd:complexType>

             

              </xsd:schema>

            </wsdl:types>

            <!-- 編寫消息的類型 -->

            <wsdl:message name="add">

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

            </wsdl:message>

            <wsdl:message name="addResponse">

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

            </wsdl:message>

            <wsdl:message name="divide">

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

            </wsdl:message>

            <wsdl:message name="divideResponse">

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

            </wsdl:message>

           

            <wsdl:message name="licenseInfo">

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

            </wsdl:message>

            <!-- 編寫輸入和輸出的類型和服務方法的接口 -->

            <wsdl:portType name="IMyService">

            <wsdl:operation name="add">

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

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

            </wsdl:operation>

           

            <wsdl:operation name="divide">

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

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

            </wsdl:operation>

            </wsdl:portType>

           

            <!-- 指定編碼格式 -->

            <wsdl:binding name="myServiceSOAP" type="tns:IMyService">

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

              <wsdl:operation name="add">

                <wsdl:input>

                  <soap:body use="literal"/>

                  <soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"/>

                </wsdl:input>

                <wsdl:output>

                  <soap:body use="literal"/>

                </wsdl:output>

              </wsdl:operation>

             

              <wsdl:operation name="divide">

                <wsdl:input>

                  <soap:body use="literal"/>

                </wsdl:input>

                <wsdl:output>

                  <soap:body use="literal"/>

                </wsdl:output>

              </wsdl:operation>

            </wsdl:binding>

           

            <!-- 編寫服務 -->

            <wsdl:service name="MyServiceImplService">

              <wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">

                <soap:address location="http://localhost:8989/ms"/>

              </wsdl:port>

            </wsdl:service>

          </wsdl:definitions>

          使用命令生成服務端代碼

          此項目的wsdl目錄/wsimport –d d:/webservice/fuwu

          把生成的代碼包復制到此項目的src

          二、根據這個文件生成一個客戶端代碼:

          1.新建一個客戶端項目

          2.執行命令:d:\>wsimport –d d:/webservice/client/ -keep –verbose http://localhost:8989/ms?wsdl

          3。把生成的客戶端的代碼包復制到客戶端項目的src

          三、編寫實現類(在實現類上指定wsdlLocation

          在服務端代碼的服務實現類中加入@WebService(endpointInterface="org.example.mywsdl.IMyService",

                targetNamespace="http://www.example.org/mywsdl/",

                wsdlLocation="META-INF/wsdl/mywsdl.wsdl")

          這幾行代碼

          四、發布服務

           

          posted on 2013-08-15 13:38 楊軍威 閱讀(558) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 兴化市| 永定县| 武平县| 鄂托克旗| 安岳县| 瑞安市| 岳阳市| 馆陶县| 虞城县| 江孜县| 通道| 铜鼓县| 仙居县| 莱西市| 吉水县| 潍坊市| 闽侯县| 丽水市| 松原市| 清河县| 金堂县| 凤凰县| 汾阳市| 嵩明县| 龙游县| 南郑县| 通榆县| 当雄县| 彭泽县| 彭山县| 延庆县| 信阳市| 天祝| 延寿县| 南宁市| 吉木乃县| 固安县| 凤台县| 濮阳县| 怀远县| 萝北县|