日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用apache CXF和maven开发Web Service

發(fā)布時間:2023/12/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用apache CXF和maven开发Web Service 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

來源: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進行構(gòu)建。

Table of Contents

  • 1 對比Axis2和CXF
  • 2 編寫服務類
  • 3 以endpoint發(fā)布
  • 4 在webapp中發(fā)布

1?對比Axis2和CXF

jws的發(fā)布對java webservice框架產(chǎn)生了巨大的影響,經(jīng)過大浪淘沙,目前java開發(fā)webservice的框架主要包括axis2和cxf。

axis2和cxf都是apache旗下的產(chǎn)品,但是其目的不同,導致webservice開發(fā)方法也不一樣。兩個框架都得到了開發(fā)者的支持。有必要對二者進行以下對比。

? Axis2 CXF
目標 WebService引擎 簡易的SOA框架,可以作為ESB
ws* 標準支持 不支持WS-Policy WS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic Profile
數(shù)據(jù)綁定支持 XMLBeans、JiBX、JaxMe 、JaxBRI、ADB JAXB, Aegis, XMLBeans, SDO, JiBX
spring集成 不支持 支持
應用集成 困難 簡單
多語言 支持C/C++ 不支持
部署 web應用 嵌入式
服務監(jiān)控和管理 支持 不支持

結(jié)論:

  • 如果希望以一種一致的方式實現(xiàn)webservice,特別是有跨語言的需求時,應該使用Axis2
  • 如果需要在現(xiàn)有的java程序(包括web應用)中增加webservice支持,應該使用CXF
  • 2?編寫服務類

    從Java6開始,WebService API從Java EE復制到了Java SE。并遵循了一系列的標準,比如JSR181(Web Service 元數(shù)據(jù)),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ù)據(jù)。下面就用javax.jws定義的標注來聲明一個WebService:

    • 創(chuàng)建maven工程
    mvn archetype:create -DgroupId=com.mycompany -DartifactId=cxfdemo -DarchetypeArtifactId=maven-archetype-webapp

    ?

    • 增加CXF依賴
    <dependency><groupId>org.apache.cxf</groupId><artifactId>apache-cxf</artifactId><version>${cxf.version}</version><type>pom</type></dependency>
    • ?配置jetty插件
    <build> <plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId></plugin></plugins></build>

    ?

    • 創(chuàng)建服務接口
    package cxfdemo;import javax.jws.WebService;@WebService public interface CXFDemo {public String sayHello(String foo); }
    • 實現(xiàn)服務類
    package cxfdemo; import javax.jws.WebService;@WebService() public class CXFDemoImpl implements CXFDemo {public String sayHello(String foo) {return "hello "+foo;}}

    3?以endpoint發(fā)布

    到目前為止,使用的都是標準Java SE中的東西。下面要開始依賴CXF實現(xiàn)一些功能。

    首先是服務的發(fā)布。CXF不僅支持通過Web容器發(fā)布WebService,也可以在嵌入式代碼中通過jetty發(fā)布WebService。

    下面的測試類包含了發(fā)布服務和客戶端調(diào)用的代碼:

    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");} }

    運行測試結(jié)果如下:

    $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
    <servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping>

    ?

    • 在web.xml中增加spring的ContextLoaderListener并配置context-param
    <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/cxfdemo-beans.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

    ?

    • beans配置文件內(nèi)容如下

    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就已經(jīng)在web容器中發(fā)布了。啟動web應用:

    $mvn jetty:run

    ?

    就可以在瀏覽器中看到已經(jīng)發(fā)布的WebService,如下圖:

    Date: 2012-12-12 15:56:07 CST

    Author: Holbrook

    Org version 7.8.11 with Emacs version 24

    Validate XHTML 1.0

    總結(jié)

    以上是生活随笔為你收集整理的使用apache CXF和maven开发Web Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。