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