利用CXF實現WebService

CXF是Apache的開源項目,可以用來進行標準wsdl文件到java代碼之間的自由轉換,方便用戶通過wsdl文件進行web服務的調用。

舉一個簡單的wsdl的例子,提供getMax的webservice。

<?xml version="1.0" encoding="UTF-8"?>  
<wsdl:definitions name="getMax" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
    xmlns:tns="http://ws.sample.com/calc/"  
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://ws.sample.com/calc/">
    <wsdl:documentation>  
       The WSDL file of SimpleService.   
    </wsdl:documentation>  
                                                                         
    <wsdl:types>  
       <wsdl:documentation>  
           Data types that are used for request and response messages.   
       </wsdl:documentation>  
       <xsd:schema targetNamespace="http://ws.sample.com/calc/">
           <xsd:element name="CalcRequest">
              <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="CalcResponse">  
              <xsd:complexType>  
                  <xsd:sequence>  
                     <xsd:element name="result" type="xsd:int" />  
                  </xsd:sequence>  
              </xsd:complexType>  
           </xsd:element>  
       </xsd:schema>  
    </wsdl:types>
                                                                         
    <wsdl:message name="CalcRequest">  
       <wsdl:documentation>  
           The data that will be transmitted to the service.   
       </wsdl:documentation>  
       <wsdl:part element="tns:CalcRequest" name="request" />  
    </wsdl:message>  
    <wsdl:message name="CalcResponse">  
       <wsdl:documentation>  
           The data that will be returned to the client.   
       </wsdl:documentation>  
       <wsdl:part element="tns:CalcResponse" name="response" />  
    </wsdl:message>  
                                                                         
    <wsdl:portType name="Service">  
       <wsdl:documentation>  
           The SumService contains the business operation.   
       </wsdl:documentation>  
       <wsdl:operation name="getMax">  
           <wsdl:documentation>  
              The operation that get the maximum of the two number.   
           </wsdl:documentation>  
           <wsdl:input message="tns:CalcRequest" />
           <wsdl:output message="tns:CalcResponse" />
       </wsdl:operation>  
    </wsdl:portType>  
    <wsdl:binding name="CalcServiceSOAP" type="tns:Service">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getMax">
        <soap:operation soapAction="http://ws.sample.com/calc/getMax"/>
        <wsdl:input>
          <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal"/>
        </wsdl:output>
      </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="CalcService">
      <wsdl:port binding="tns:CalcServiceSOAP" name="CalcServiceSOAP">
        <soap:address location="http://ws.sample.com/calc/"/>
      </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

通過CXF提供的wsdl2java工具將wsdl分別轉換爲server端和client端代碼:

wsdl2java -d src -server -frontend jaxws21 max.wsdl
wsdl2java -d src -client -frontend jaxws21 max.wsdl


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