wsdl中添加soap:header元素

轉自:http://www.haogongju.net/art/1722802


我們在開發webservice的時候,經常會看到有些生成的wsdl文件中包含了<soap:header>節點元素,那麼怎樣在wsdl

 

文件中添加<soap:header>節點元素。

 

我們知道開發webservice服務其實有兩種方式cxf發佈有兩種方式,一種Object first,也就是普通的方式,還有一種是

 

WSDLfirst,就是你自己編寫WSDL文檔然後發佈。如果需要在生成的wsdl文件中包含soap:header元素,那麼我們只

 

能採用第二種方式來開發webservice服務。

 

我將採用的是http://liuwuhen.iteye.com/blog/1666189 該博客中所產生的wsdl文件的基礎,進行soap:header元

 

素。

 

具體實現的步驟如下:


1.聲明元素。

 

<xsd:element name="header" type="tns:headertype"/>
          <xsd:complexType name="headertype">
     <xsd:sequence>
      <xsd:element name="spPassword" type="xsd:string" />
      <xsd:element name="spId" type="xsd:int" />
     </xsd:sequence>
</xsd:complexType>

 

我們可以這樣理解,其實xsd:element就相當於我們一個類中所對應的屬性, xsd:complexType 可以理解爲屬性所對應

 

類型。

 

2. 在message節點處引用該頭部元素

 

<wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters"/>
    <wsdl:part element="tns:header" name="header_info"/>
</wsdl:message>

 

其中 element="tns:header"引用的是上面聲明的元素。

 

3.在wsdl:binding中的wsdl:operation處添加<soap:header>元素

 

<wsdl:operation name="sayHello">
   <soap:operation soapAction="" style="document" />
   <wsdl:input name="sayHello">
      <soap:header message="tns:sayHello"
     part="header_info" use="literal" wsdl:required="true"/>
    <soap:body use="literal" parts="sayHello"/>
   </wsdl:input>
   <wsdl:output name="sayHelloResponse">
    <soap:body use="literal" />
   </wsdl:output>
  </wsdl:operation>

 

其中part引用是message部分聲明的節點名稱,use=literal表示的是編碼的方式.

 

4.修改發佈服務的cxf配置文件。

 

<jaxws:endpoint implementor="com.liuwuhen.cxf.IHelloWorldImpl"
  address="/sayHello" wsdlLocation="WEB-INF/HelloWorld.wsdl">

 

wsdlLocation :即指定wsdl文件所在的路徑。

 

通上述的三個步驟即可完成wsdl文件中添加soap:header節點元素,應該很簡單。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章