CXF是什么?
CXF是建立在SOAP協議上的一個Web service框架。什么事SOAP協議,簡單來說就是兩個不同項目(開發語言不同等)通過xml文件來描述要傳輸的東西,然后通過HTTP協議傳輸,接收方把收到的xml解析成需要的對象使用,返回的時候又用xml封裝又通過http協議傳輸,如此就是SOAP協議。而CXF就是兩個項目之間為了提供服務,而開發的一個開源框架,使用CXF框架可以快速搭建web service。CXF就是將項目中暴露出來的接口(服務)包裝起來,成為wsdl,使用方通過wsdl來調用這些服務。
跟CXF同樣功能的是XFIRE,AXIAS。可是現在都用CXF了,他整合了XFIRE。AXIAS也越來越少人用。
CXF的demo:
第一步:下載CXF:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7.7/apache-cxf-2.7.7.zip 。解壓后,新建一個Java項目,導入cxf中lib文件夾的所有jar包。
第二步:編寫接口CalculatorService.java,這個接口就是提供web服務的那個接口啦,里面的方法都是提供給人家調用滴。
package com.likunjie; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface CalculatorService {int add(int a, int b); String concat(String a, String b); }
第三步:編寫接口的實現類CalculatorServiceImpl.java,類里面要實現接口的方法。
package com.likunjie; import javax.jws.WebService; @WebService(endpointInterface="com.demo.CalculatorService",serviceName="calculator") public class CalculatorServiceImpl implements CalculatorService { @Override public int add(int a, int b) { return a + b; } @Override public String concat(String a, String b) { return a + b; } }
第四步:開發服務端WebService.java,寫完以后右擊,運行Run As Java application(之后也不要關掉)。如果沒有錯誤就繼續下一步,有錯誤檢查上一個類注解的地址是否跟你包名一致。檢查是否有拼寫錯誤。
package com.likunjie; import javax.xml.ws.Endpoint; public class WebService { public static void main(String[] args) { System.out.println("web service start"); CalculatorServiceImpl implementor = new CalculatorServiceImpl(); String address = "http://localhost:8080/calculator"; //這是上面在注解中已經聲明過的URL Endpoint.publish(address, implementor); System.out.println("web service started"); } }
第五步:打開瀏覽器,輸入:http://localhost:8080/calculator?wsdl ,如果顯示以下信息,那么你已經完成了一大半。這個就是傳說中的wsdl,要使用服務的客戶端要先獲取這個wsdl,知道里面描述了有什么服務使用以后。再根據這個wsdl,可以生成自己的客戶端,並且訪問所描述的服務。(這個一個schema文件,不懂的話再要去學一下)
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://likunjie.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="calculator" targetNamespace="http://likunjie.com/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://likunjie.com/" elementFormDefault="unqualified" targetNamespace="http://likunjie.com/" version="1.0"> <xs:element name="add" type="tns:add" /> <xs:element name="addResponse" type="tns:addResponse" /> <xs:element name="concat" type="tns:concat" /> <xs:element name="concatResponse" type="tns:concatResponse" /> <xs:complexType name="concat"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string" /> <xs:element minOccurs="0" name="arg1" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="concatResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="add"> <xs:sequence> <xs:element name="arg0" type="xs:int" /> <xs:element name="arg1" type="xs:int" /> </xs:sequence> </xs:complexType> <xs:complexType name="addResponse"> <xs:sequence> <xs:element name="return" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="concatResponse"> <wsdl:part element="tns:concatResponse" name="parameters"></wsdl:part> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part element="tns:addResponse" name="parameters"></wsdl:part> </wsdl:message> <wsdl:message name="add"> <wsdl:part element="tns:add" name="parameters"></wsdl:part> </wsdl:message> <wsdl:message name="concat"> <wsdl:part element="tns:concat" name="parameters"></wsdl:part> </wsdl:message> <wsdl:portType name="CalculatorService"> <wsdl:operation name="concat"> <wsdl:input message="tns:concat" name="concat"></wsdl:input> <wsdl:output message="tns:concatResponse" name="concatResponse"></wsdl:output> </wsdl:operation> <wsdl:operation name="add"> <wsdl:input message="tns:add" name="add"></wsdl:input> <wsdl:output message="tns:addResponse" name="addResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="calculatorSoapBinding" type="tns:CalculatorService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="concat"> <soap:operation soapAction="" style="document" /> <wsdl:input name="concat"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="concatResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="add"> <soap:operation soapAction="" style="document" /> <wsdl:input name="add"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="addResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="calculator"> <wsdl:port binding="tns:calculatorSoapBinding" name="CalculatorServiceImplPort"> <soap:address location="http://localhost:8080/calculator" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
第六步:客戶端是完全不知道服務端以上的代碼的。只有cxf和被提供的這個wsdl,那么怎樣才能調用服務呢?CXF包的bin目錄下其實有一個指令,叫wsdl2java,我們可以使用這個命令,根據得到的wsdl來生成java代碼。接下來,小心跟着下面的步驟:1、打開命令行,路徑切換到cxf包下的bin目錄(是為了使用該目錄下的指令)。2、在命令行敲上:wsdl2java http://localhost:8080/calculator?wsdl 。完了以后再看這個bin目錄,你會發現多了一個com文件夾,這個就是產生的項目代碼文件,留着等下用。3、新建一個項目作為客戶端,同樣把所有CXF的jar包都導入,接着把上一步產生的com文件夾剪切到項目的src目錄下。 把有出現紅線的地方按提示修改就行了。4、在同一個包下面編寫Client類,在main方法調用服務。最后運行Run as Java Application。
package com.likunjie; public class Client { public static void main(String[] args) { Calculator calc = new Calculator(); CalculatorService service = calc.getCalculatorServiceImplPort(); int a = service.add(3, 7); String b = service.concat("what's ", "that about?"); System.out.println(a); System.out.println(b); } }
輸出結果:
10
what's that about?
如果有問題,可以留下評論。