天天看點

使用 Apache CXF 建立 WebService 總結

轉載請注明出處:http://blog.csdn.net/oathevil/article/details/7520732

J2EE中WebService的建立可以使用Axis2,也可以使用Apache CXF,兩者的差別與優缺點具體見附檔1。

最近花了點時間大概看了一下WebService相關的資料,現就Apache CXF的使用過程作如下總結:

使用者可以遵循以下步驟進行WebService工程的建立。

1. 下載下傳 Apache CXF ,可以去官方下載下傳 http://cxf.apache.org/ (本文使用的是2.3.9的版本)。

2. 配置Apache CXF相關環境變量:

  a. 建立一個CXF_HOEM變量,值為CXF架構所在根目錄(如本文為E:\Java\apache-cxf-2.3.9);

  b. 向CLASSPATH、PATH加入環境變量。具體:CLASSPATH=%CXF_HOME%\lib; PATH=%CXF_HOME%\bin;

測試環境變量是否設定成功可在CMD中輸入java2ws、wsdl2java等測試指令是否有效。

3. 在Eclipse -> Window -> Preferences -> Web Services -> CXF 2.x Preferences -> CXF Runtime 中另入CFX Rutime庫(Add -> 選擇CXF的根目錄即可)。

4. 通過以上配置後,即可在Eclipse中 File -> New -> Other -> Web Services -> Web Service 中建立Web Service 服務。或者也可通過 Right Click 相應的接口檔案,Web Services -> Create Web Service來建立,如圖:

使用 Apache CXF 建立 WebService 總結
使用 Apache CXF 建立 WebService 總結

之後再選擇Implementation檔案,其餘基本使用預設配置即可。

這樣以後即可生成相應的ResponseWrapper、RequestWrapper、.wsdl檔案和.xsd檔案。

注意:

1.隻有當配置檔Web.xml、beans.xml、applicationContent.xml等正确配置時,才會生成成功。上述文檔配置可參見附檔2;

2.當包含WebService的項目WAR被部署之後,則相應的WebService也自動釋出,無需進行額外釋出。

Apache CXF 結合 Spring 使用的例子将後續附上。

附檔1

正好現在在學webService.可以共同進步啊

Web Services 架構如 Axis2、CXF 都是由現有的項目中逐漸演化而來的,Axis2 是由 Axis 1.x 系列演化過來,而 Apache CXF 則是由 Celtix 和 XFire 項目整合而生,并且剛剛釋出了 2.0.2 的最新版本,不過仍是 Apache 的一個孵化項目。

Axis2 是對 Axis 進行了徹底的重寫的一個新項目了,它使用了新的子產品化架構,更友善于功能性的擴充等等。

Apache CXF 則是由 XFire 和 Celtix 兩個現有的項目進行了重組。

先比較一下它們的不同之處:

1、Apache CXF 支援 WS-Addressing、WS-Policy、WS-RM、WS-Security和WS-I BasicProfile

2、Axis2 支援 WS-Addressing、WS-RM、WS-Security和WS-I BasicProfile,WS-Policy将在新版本裡得到支援

3、Apache CXF 是根據Spring哲學來進行編寫的,即可以無縫地與Spring進行整合

4、Axis2 不是

5、Axis2 支援更多的 data bindings,包括 XMLBeans、JiBX、JaxMe 和 JaxBRI,以及它原生的 data binding(ADB)。

6、Apache CXF 目前僅支援 JAXB 和 Aegis,并且預設是 JAXB 2.0,與 XFire 預設是支援 Aegis 不同,XMLBeans、JiBX 和 Castor 将在 CXF 2.1 版本中得到支援,目前版本是 2.0.2

7、Axis2 支援多種語言,它有 C/C++ 版本。

8、Apache CXF 提供友善的Spring整合方法,可以通過注解、Spring标簽式配置來暴露Web Services和消費Web Services

如何抉擇:

1、如果應用程式需要多語言的支援,Axis2 應當是首選了;

2、如果應用程式是遵循 Spring 哲學路線的話,Apache CXF 是一種更好的選擇,特别對嵌入式的 Web Services 來說;

3、如果應用程式沒有新的特性需要的話,就仍是用原來項目所用的架構,比如 Axis1,XFire,Celtrix 或 BEA 等等廠家自己的 Web Services 實作,就别勞民傷财了

因為CXF可以和Spring無縫的進行結合,而我的項目用到了spring ,是以我選的是CXF

附檔2

1.CXF本身就使用了Spring的東西,它和Spring內建可謂是無縫內建

主要讓釋出服務的任務交spring去釋出

2.配置web.xml

配置spring

Xml代碼

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>WEB-INF/beans.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>
  7. org.springframework.web.context.ContextLoaderListener
  8. </listener-class>
  9. </listener>

配置我們的cxf

Xml代碼

  1. <!-- 配置CXF Servlet -->
  2. <servlet>
  3. <servlet-name>CXFServlet</servlet-name>
  4. <servlet-class>
  5. org.apache.cxf.transport.servlet.CXFServlet
  6. </servlet-class>
  7. <load-on-startup>1</load-on-startup>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>CXFServlet</servlet-name>
  11. <url-pattern>/*</url-pattern>
  12. </servlet-mapping>

3.beans.xml

Xml代碼

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!-- START SNIPPET: beans -->
  3. <beansxmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:jaxws="http://cxf.apache.org/jaxws"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  9. <!--會向cxf jar包去找 -->
  10. <importresource="classpath:META-INF/cxf/cxf.xml"/>
  11. <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  12. <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  13. <!--
  14. <jaxws:endpoint
  15. id="helloWorld"
  16. implementor="com.cxf.server.HelloWorldImpl"
  17. address="/HelloWorld"/>
  18. -->
  19. <!-- 另一種釋出方式 -->
  20. <beanid="hello"class="com.cxf.server.HelloWorldImpl"/>
  21. <jaxws:endpointid="helloWorld"
  22. implementor="#hello"
  23. address="/HelloWorld">
  24. </jaxws:endpoint>
  25. <beanid="us"class="com.cxf.users.UserServiceImpl"></bean>
  26. <jaxws:endpointid="userService"
  27. implementor="#us"
  28. address="/userAction"
  29. >
  30. <jaxws:inInterceptors>
  31. <beanclass="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  32. </jaxws:inInterceptors>
  33. <jaxws:outInterceptors>
  34. <beanclass="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  35. </jaxws:outInterceptors>
  36. </jaxws:endpoint>
  37. </beans>
  38. <!-- END SNIPPET: beans -->

<jaxws:endpoint 通過他們進行服務釋出,通過其指定的address通路你的wsdl

Xml代碼

  1. <jaxws:inInterceptors>
  2. <beanclass="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  3. </jaxws:inInterceptors>

可以加入相關interceptor

By  M

2012-04-28