使用apache CXF和maven开发Web Service
來源:http://www.cnblogs.com/holbrook/archive/2012/12/12/2814821.html
目前主要的java webservice框架剩下了axis2和cxf。本文對兩個框架的目標、標準支持、開發(fā)和部署等方面進行了簡單的對比。對于在現(xiàn)有web應用中發(fā)布webservice,本文建議使用cxf。 更進一步,本文介紹了cxf的嵌入式代碼和web容器兩種發(fā)布方式。
本文中的例子使用maven進行構建。
Table of Contents
- 1 對比Axis2和CXF
- 2 編寫服務類
- 3 以endpoint發(fā)布
- 4 在webapp中發(fā)布
1?對比Axis2和CXF
jws的發(fā)布對java webservice框架產生了巨大的影響,經過大浪淘沙,目前java開發(fā)webservice的框架主要包括axis2和cxf。
axis2和cxf都是apache旗下的產品,但是其目的不同,導致webservice開發(fā)方法也不一樣。兩個框架都得到了開發(fā)者的支持。有必要對二者進行以下對比。
| 目標 | WebService引擎 | 簡易的SOA框架,可以作為ESB |
| ws* 標準支持 | 不支持WS-Policy | WS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic Profile |
| 數(shù)據綁定支持 | XMLBeans、JiBX、JaxMe 、JaxBRI、ADB | JAXB, Aegis, XMLBeans, SDO, JiBX |
| spring集成 | 不支持 | 支持 |
| 應用集成 | 困難 | 簡單 |
| 多語言 | 支持C/C++ | 不支持 |
| 部署 | web應用 | 嵌入式 |
| 服務監(jiān)控和管理 | 支持 | 不支持 |
結論:
2?編寫服務類
從Java6開始,WebService API從Java EE復制到了Java SE。并遵循了一系列的標準,比如JSR181(Web Service 元數(shù)據),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件標準)等。 并分別定義到javax.jws, javax.xml.ws 和 javax.xml.soap包中。
JSR181支持使用標注(annotation)來定義WebService。在javax.jws中主要的標注類包括:
| WebService | 將 Java 類標記為實現(xiàn) Web Service,或者將 Java 接口標記為定義 Web Service 接口 |
| WebMethod | 定制Web Service方法 |
| WebParam | 定制Web Service方法的參數(shù) |
| WebResult | 定制Web Service方法的返回值 |
| SOAPBinding | 指定WebService的SOAP映射樣式 |
使用標注可以在不改變代碼邏輯的前提下讓外部代碼能夠獲得更多的元數(shù)據。下面就用javax.jws定義的標注來聲明一個WebService:
- 創(chuàng)建maven工程
?
- 增加CXF依賴
- ?配置jetty插件
?
- 創(chuàng)建服務接口
- 實現(xiàn)服務類
3?以endpoint發(fā)布
到目前為止,使用的都是標準Java SE中的東西。下面要開始依賴CXF實現(xiàn)一些功能。
首先是服務的發(fā)布。CXF不僅支持通過Web容器發(fā)布WebService,也可以在嵌入式代碼中通過jetty發(fā)布WebService。
下面的測試類包含了發(fā)布服務和客戶端調用的代碼:
package cxfdemo.test;import javax.xml.ws.Endpoint;import junit.framework.Assert; import junit.framework.TestCase;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import cxfdemo.CXFDemo; import cxfdemo.CXFDemoImpl;public class TestEndpoint extends TestCase {private static final String ADDRESS = "http://localhost:9000/cxfdemo"; protected void setUp() throws Exception {super.setUp();System.out.println("Starting Server"); CXFDemoImpl demo = new CXFDemoImpl(); Endpoint.publish(ADDRESS, demo);System.out.println("Start success");}public void testSayHello(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(CXFDemo.class);factory.setAddress(ADDRESS);CXFDemo client = (CXFDemo)factory.create();Assert.assertEquals(client.sayHello("foo"), "hello foo");} }運行測試結果如下:
$mvn test ... ... -------------------------------------------------------T E S T S ------------------------------------------------------- Running cxfdemo.test.TestEndpoint Starting Server 2012-12-12 11:29:02 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass ???: Creating Service {http://cxfdemo/}CXFDemoImplService from class cxfdemo.CXFDemo 2012-12-12 11:29:03 org.apache.cxf.endpoint.ServerImpl initDestination ???: Setting the server's publish address to be http://localhost:9000/cxfdemo 2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info ???: jetty-7.4.2.v20110526 2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info ???: Started SelectChannelConnector@localhost:9000 STARTING 2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info ???: started o.e.j.s.h.ContextHandler{,null} Start success 2012-12-12 11:29:04 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass ???: Creating Service {http://cxfdemo/}CXFDemoService from class cxfdemo.CXFDemo Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.076 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ... ...4?在webapp中發(fā)布
CXF提供了spring的集成,同時還提供了org.apache.cxf.transport.servlet.CXFServlet用于在web容器中發(fā)布WebService。 前面的例子中增加了整個apache-cxf的依賴,所以會自動增加對srping的引用。只需要寫beans配置文件和web.xml文件即可。
- 在web.xml中配置CXFServlet
?
- 在web.xml中增加spring的ContextLoaderListener并配置context-param
?
- beans配置文件內容如下
cxfdemo-beans.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" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" ><jaxws:endpoint id="cxfDemo" implementor="cxfdemo.CXFDemoImpl" address="/cxfdemo" /> </beans>?
如此,WebService就已經在web容器中發(fā)布了。啟動web應用:
$mvn jetty:run?
就可以在瀏覽器中看到已經發(fā)布的WebService,如下圖:
Date: 2012-12-12 15:56:07 CST
Org version 7.8.11 with Emacs version 24
Validate XHTML 1.0總結
以上是生活随笔為你收集整理的使用apache CXF和maven开发Web Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行卡交易密码忘了怎么办 去银行修改密码
- 下一篇: python数据类型有哪几种?(利用Py