Apache CXF 應用小結

最新項目中需要用到webservices發佈接口,之前項目用的都是xfire,由於框架中Spring版本原因,xfire用不起來了,然後去搜了下,開源中國裏找到了這段話:

如果你要在新項目裏用XFire,請改用CXF.CXF是XFire的延續,可以被看作是XFire2.0。CXF有很多新特性,且修復了很多XFire的bug,更重要的是CXF兼容JAX-WS!XFire將繼續被維護和修復bug,但是新功能的開發都轉移到了CXF上。詳細信息請參考XFire/Celtix合併聲明和CXF官網

才發現xfire早在幾年前就停止更新啦,轉而替代產品是CXF(突然發現公司框架好老土!),學習了下CXF,發現確實好用,配置簡單,調用方便,具體貌似還有一些強大的功能,暫時沒用到,不去深究,下面來簡單介紹下CXF的具體使用。

一、CXF項目下載

1、首先CXF的官網地址 : http://cxf.apache.org/
注:當前最新版本是3.1.2,我用的是milestone版本3.0.0

2、下載相關壓縮包(大約40-50M),解壓縮到本地,這邊主要用到裏面的jar,當然不下載也行,關鍵方便學習嘛

二、項目集成

1、項目所需jar包(倆種方式)

第一種方式: 可以直接通過maven配置下載,具體配置如下:

<cxf.version>3.0.0-milestone1
</cxf.version>
<dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-rt-frontend-jaxws</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-rt-transports-http</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-tools-common</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-tools-java2ws</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-tools-validator</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-tools-wsdlto-core</artifactId>
       <version>${cxf.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>
       <version>${cxf.version}</version>
    </dependency>

第二種方式,直接通過下載下的壓縮包,解壓獲取相關jar包,具體使用到的jar包如下:

cxf-core-3.0.0-milestone1.jar
cxf-rt-bindings-soap-3.0.0-milestone1.jar
cxf-rt-bindings-xml-3.0.0-milestone1.jar
cxf-rt-databinding-aegis-3.0.0-milestone1.jar
cxf-rt-databinding-jaxb-3.0.0-milestone1.jar
cxf-rt-frontend-jaxws-3.0.0-milestone1.jar
cxf-rt-frontend-simple-3.0.0-milestone1.jar
cxf-rt-javascript-3.0.0-milestone1.jar
cxf-rt-transports-http-3.0.0-milestone1.jar
cxf-rt-ws-addr-3.0.0-milestone1.jar
cxf-rt-wsdl-3.0.0-milestone1.jar
cxf-rt-ws-policy-3.0.0-milestone1.jar
cxf-tools-common-3.0.0-milestone1.jar
cxf-tools-java2ws-3.0.0-milestone1.jar
cxf-tools-validator-3.0.0-milestone1.jar
cxf-tools-wsdlto-core-3.0.0-milestone1.jar
cxf-tools-wsdlto-databinding-jaxb-3.0.0-milestone1.jar
cxf-tools-wsdlto-frontend-jaxws-3.0.0-milestone1.jar
neethi-3.0.2.jar
stax2-api-3.1.1.jar
woodstox-core-asl-4.2.0.jar
wsdl4j-1.6.3.jar
xml-apis-1.0.b2.jar
xml-resolver-1.2.jar
xmlschema-core-2.0.3.jar

2、服務端webservices接口發佈

首先配置web.xml

 <!-- CXF 配置 -->
 <servlet>  
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
 </servlet>  

 <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/ ws/*</url-pattern>  
 </servlet-mapping>

然後編寫業務接口(UserInfoService):

package com.quangao.service.webservice; 

import javax.jws.WebService;

/**
 * Business Service Interface to handle communication between web and
 * persistence layer.
 * 
 */
@WebService
public  interface UserInfoService {

  String getUser(); 

}

有接口必然要有接口實現(UserInfoServiceImpl):

package com.quangao.service.webservice.impl; 

import javax.jws.WebService; 
import com.quangao.service.webservice.UserInfoService; 

@WebService(endpointInterface = "com.quangao.service.webservice.UserInfoService", targetNamespace = " http://webservice.service.quangao.com/"
public class UserInfoServiceImpl implements UserInfoService { 

      public String getUser()
      { 
              return " 錢猛 ";
      }
}

接口寫好之後就可以發佈接口啦,具體配置如下(application.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:p=" http://www.springframework.org/schema/p "
      xmlns:tx=" http://www.springframework.org/schema/tx "
      xmlns:aop=" http://www.springframework.org/schema/aop "
      xmlns:jaxws=" http://cxf.apache.org/jaxws "
      xmlns:cxf=" http://cxf.apache.org/core "
      xsi:schemaLocation= " http://www.springframework.org/schema/beans
      http: //www.springframework.org/schema/beans/sprin g-beans-3.0.xsd
      http: //www.springframework.org/schema/tx
      http: //www.springframework.org/schema/tx/sprin g-tx-3.0.xsd
      http: //www.springframework.org/schema/aop
      http: //www.springframework.org/schema/aop/sprin g-aop-3.0.xsd
      http: //cxf.apache.org/jaxws    
      http: //cxf.apache.org/schemas/jaxws.xsd" >

    <!-- 配置請參考官網: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html --
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="getInfoServiceImpl" class="com.quangao.service.webservice.impl.UserInfoServiceImpl" />

    <!-- JAX-WS -->
    <!-- implementor 指定 WebService 實現類, address 指定訪問地址 -->
    <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService" publish="true" />
</beans>

發佈完成後要進行測試發佈接口是否成功,具體步驟如下:

1、根據web.xml中配置,在瀏覽器中輸入webservice發佈鏈接
例如http://localhost:8080/jssc_platform/ws
出現接口頁面:
這裏寫圖片描述

2、點擊方法鏈接進入wsdl文件

這裏寫圖片描述

現在服務端發佈到此結束,下面就開始有客戶端進行調用啦,調用方式有兩種,一種是通用調用方式,一種是代理調用

通用調用方式:

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 方式1. 指定WSDL文件的url地址
org.apache.cxf.endpoint.Client client = dcf
        .createClient("http://localhost:8080/jssc-web/ws/getInfoService?wsdl");
// 方式2. 指定WSDL文件的絕對路徑
// org.apache.cxf.endpoint.Client client = dcf.createClient("E:/cxf/getInfoService.wsdl");
// 方式3. classpath下的WSDL文件
// org.apache.cxf.endpoint.Client client = dcf.createClient("getInfoService.wsdl");
Object[] info = client.invoke("getUser");
if (info != null) {
    System.out.println("cxf 返回結果:" + info[0]);
}

代理調用方式:
1、打開CMD命令行窗口,切換到apache-cxf-3.0.0/bin目錄下,輸入命令:

wsdl2java -p cn.com.webxml -d f:/ http://localhost:8080/ws//getInfoService?wsdl

注:-p 表示生成的JAVA客戶端調用代碼的包路徑;-d 表示生成在本地哪個目錄下(本例爲F盤根目錄); 最後一個參數爲WSDL文件路徑(也可以是本地文件)

2、利用生成的工具類,直接調用webservices接口

UserInfoService_Service service = new UserInfoService_Service();
UserInfoService servicesop = service.getUserInfoServiceImplPort();
String userName = servicesop.getUser();

針對這倆種調用方式,我比較喜歡和常用的方式是第二種,這樣可以根據用戶提供的webservices接口直接生成本地接口文件,調用非常方便。

以上就是CXF應用的簡單小結,個人認爲比之前的Xfire好用多啦!

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