Apache CXF入門

分類:IT技術 時間:2017-09-25

  CXF簡介

  Apache CXF = Celtix + XFire,開始叫 Apache CeltiXfire,後來更名為 Apache CXF 了。CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,提供了對 JAX-WS 全面的支持,並且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支持,並且可以根據實際項目的需要,采用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕松地實現 Web Services 的發布和使用。Apache CXF已經是一個正式的Apache頂級項目。

  Apache CXF 是一個開源的 Services 框架,CXF 幫助您利用 Frontend 編程 API 來構建和開發 Services ,像 JAX-WS 。這些 Services 可以支持多種協議,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,並且可以在多種傳輸協議上運行,比如:HTTP、JMS 或者 JBI,CXF 大大簡化了 Services 的創建,同時它繼承了 XFire 傳統,一樣可以天然地和 Spring 進行無縫集成。

  CXF優點

  (1)支持多種協議:

    SOAP1.1,1.2

    XML/HTTP

    CORBA(Common Object Request Broker Architecture公共對象請求代理體系結構,早期語言使用的WS。C,c++,C#)

  (2)可以與Spring進行快速無縫的整合

  (3)靈活的部署:可以運行在Tomcat,Jboss,Jetty(內置),IBMWS,BeaWL上面。

   看這個之前可以看看關於WebService的文章:鏈接http://www.cnblogs.com/xiaobai1226/p/7543965.html

  接下來就開始寫一個CXF的簡單入門程序

 

  一、服務端程序

  首先去官網下載jar包

  官方下載網址:http://cxf.apache.org/download.html

  進入此頁面後,會看到此頁面

 

  下載好後會看到這樣一個壓縮文件:

  解壓後,有這些目錄文件

  下面簡單介紹一下這些目錄以及裏面文件的作用

   (1)bin:是 CXF 框架中所提供的代碼生成、校驗、管理控制臺工具:

   (2)docs:CXF 所有類(class)對應的 API 文檔,為開發者使用 CXF 完成應用開發提供應有的幫助

   (3)etc:包含一個基本的 Service 暴露所需要的 web.xml 文件,及其它的配置文件

   (4)lib:目錄中包含 CXF 及其運行時所需要的和可選的第三方支持類包(.jar 文件),可以根據不同項目所需的 CXF 特性選擇所需要的支持類包

   (5)licenses:列表了引用第三方 jar 包的相關許可協議

   (6)samples:samples 目錄中包含了所有隨 CXF 二進制包發布的示例,包含這些示例的源代碼和相關 Web 應用配置文件,可以方便地用 Ant 來編譯運行測試這些示例,來了解 CXF 的開發和使用的方法。可以通過 samples 目錄和它各個子目錄下的 README.txt 的文件來詳細了解示例的編譯與運行的步驟

   (7)LICENSE :文件中包含了 CXF 框架的授權協議 Apache License version 2.0 。

   (8)NOTICE :羅列了 CXF 框架用到的相關第三方組件的授權協議以其它的相關信息

   (9)README :文件中包含了 CXF 框架本身的一些簡要說明。

     (10)release_notes.txt :包含了 CXF 發布時的一些信息,包括運行時所需要的環境,修復 BUG 的列表等

  接下來,第一步:創建動態web項目

  第二步:導入CXF相關jar包,以下jar包是使用CXF所必須的jar包(CXF能運行起來最基本的jar包)

 

  第三步:在web.xml中配置CXF框架提供的一個Servlet

  <!-- 配置CXF框架提供的Servlet -->
  <servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <!-- 通過初始化參數指定CXF框架的配置文件位置 -->
      <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

  第四步:在類路徑下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">
                    
    <!-- 引入CXF Bean定義 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    
</beans>

  第五步:開發一個接口和實現類

  接口,接口上必須使用@WebService實現類(jdk1.6及以後才支持@WebService註解,所以要確保jdk版本夠高)

import Javax.jws.WebService;

@WebService
public interface TestCXFService {
    public String firstCXF(String name);
}

  實現類

public class TestCXFServiceImpl implements TestCXFService{
    @Override
    public String firstCXF(String name) {
        system.out.println("基於CXF開發的服務端firstCXF被調用了");
        return "Hello "+name;
    }
}

  第六步:在cxf.xml中註冊服務

<bean id="firstCXFService" class="com.cxf.service.impl.TestCXFServiceImpl"/>
<!-- 註冊服務  -->
<jaxws:server id="myCXFService" address="/cxfService">
    <jaxws:serviceBean>
        <ref bean="firstCXFService"/>
    </jaxws:serviceBean>
</jaxws:server>

  到現在,使用CXF配置的服務就配置成功了

  接下來,啟動tomcat,在瀏覽器訪問這個地址:http://localhost:8080/testCXF/service/cxfService?wsdl

  http://配置此服務的服務器IP地址 / 端口 / 你所創建的工程的名稱 / 自己在web.xml配置的路徑  / 自己配置的address?wsdl  註意:(1)由於我使用tomcat所以端口號為8080       (2)<url-pattern>/service/*</url-pattern>         

(3)address="/cxfService"

  若出現這個頁面說明成功了

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.cxf.com/" name="TestCXFServiceImplService" targetNamespace="http://impl.service.cxf.com/">
<wsdl:import location="http://localhost:8080/testCXF/service/cxfService?wsdl=TestCXFService.wsdl" namespace="http://service.cxf.com/"></wsdl:import>
<wsdl:binding name="TestCXFServiceImplServiceSoapBinding" type="ns1:TestCXFService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="firstCXF">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="firstCXF">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="firstCXFResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestCXFServiceImplService">
<wsdl:port binding="tns:TestCXFServiceImplServiceSoapBinding" name="TestCXFServiceImplPort">
<soap:address location="http://localhost:8080/testCXF/service/cxfService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

   二、客戶端程序

  有兩種方式:方式一:使用jdk提供的wsimport命令生成本地代碼完成調用(另一篇介紹webservice的文章中有使用,鏈接在文章開頭給出了,在這裏不再使用)

        方式二:使用CXF提供的方式(我們使用第二種方式)    

  第一步:創建Java項目並導入CXF相關jar包 (jar包同上)

  第二步:使用wsimport或者CXF提供wsdl2java生成本地代碼,只需要生成接口文件

  打開cmd進入CXF文件夾的bin目錄下(如apache-cxf-3.2.0\bin),輸入

wsdl2java -d . -p com.cxf.client http://localhost:8080/testCXF/service/cxfService?wsdl

  .代表將生成的文件放到當前目錄下,-p代表生成的目錄結構,最後的地址代表根據哪個wsdl生成文件

  敲回車,出現以下結果,和方式一不同這個成功後,沒有任何提示

  這時就發現,當前目錄下出現了這個文件夾

  打開後,有以下目錄文件,而我們只需要用到一個接口文件(紅框標出來的)

  

  第三步:將接口文件復制到項目中

  

  復制過來後,我們需要將紅框內內容刪掉,因為我們只把一個接口文件復制過來了,其他的並沒有動,所以找不到這個文件,而我們又用不到,所以直接刪掉就好  

  刪除後接口內代碼是這樣的

package com.cxf.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.2.0
 * 2017-09-24T12:35:54.073+08:00
 * Generated source version: 3.2.0
 * 
 */
@WebService(targetNamespace = "http://service.cxf.com/", name = "TestCXFService")
@XmlSeeAlso({})
public interface TestCXFService {

    @WebMethod
    @RequestWrapper(localName = "firstCXF", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXF")
    @ResponseWrapper(localName = "firstCXFResponse", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXFResponse")
    @WebResult(name = "return", targetNamespace = "")
    public java.lang.String firstCXF(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}

  第四步:提供spring配置文件,註冊客戶端代理對象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">
                    
    <!-- 引入CXF Bean定義如下 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    
    <!-- 註冊CXF客戶端代理對象,通過spring框架創建這個代理對象,使用代理對象實現遠程調用 -->
    <jaxws:client id="myCXFClient" address="http://localhost:8080/testCXF/service/cxfService" serviceClass="com.cxf.client.TestCXFService">
        
    </jaxws:client>
</beans>

  第五步:寫一個測試類,讀取spring配置文件,創建spring工廠,從工廠中獲取代理對象,實現遠程調用

package com.cxf.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCXFClient {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:cxf.xml");
        TestCXFService proxy = (TestCXFService) ctx.getBean("myCXFClient");
        String result = proxy.firstCXF("my first CXFClient");
        System.out.println(result);
    }
}

  執行main方法(此時要確保服務端啟動著),出現以下結果,就證明程序運行成功了

  客戶端控制臺打印

        

  服務端控制臺打印

        

  到現在,CXF入門程序就完成了。

 

  

 

  

 

 


Tags: 可以 Apache CXF Services 多種 支持

文章來源:


ads
ads

相關文章
ads

相關文章

ad