国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用 CXF 做 webservice 簡(jiǎn)單例子

2019-11-11 03:16:21
字體:
供稿:網(wǎng)友

一、對(duì)比Axis2和CXF

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

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

 
 Axis2CXF
目標(biāo)WebService引擎簡(jiǎn)易的SOA框架,可以作為ESB
ws* 標(biāo)準(zhǔn)支持不支持WS-PolicyWS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic PRofile
數(shù)據(jù)綁定支持xmlBeans、JiBX、JaxMe 、JaxBRI、ADBJAXB, Aegis, XMLBeans, SDO, JiBX
spring集成不支持支持
應(yīng)用集成困難簡(jiǎn)單
多語言支持C/C++不支持
部署web應(yīng)用嵌入式
服務(wù)監(jiān)控和管理支持不支持

結(jié)論:

如果希望以一種一致的方式實(shí)現(xiàn)webservice,特別是有跨語言的需求時(shí),應(yīng)該使用Axis2如果需要在現(xiàn)有的java程序(包括web應(yīng)用)中增加webservice支持,應(yīng)該使用CXF

二、編寫服務(wù)類

從Java6開始,WebService API從Java EE復(fù)制到了Java SE。并遵循了一系列的標(biāo)準(zhǔn),比如JSR181(Web Service 元數(shù)據(jù)),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件標(biāo)準(zhǔn))等。 并分別定義到j(luò)avax.jws, javax.xml.ws 和 javax.xml.soap包中。

JSR181支持使用標(biāo)注(annotation)來定義WebService。在javax.jws中主要的標(biāo)注類包括:

 
標(biāo)注說明
WebService將 Java 類標(biāo)記為實(shí)現(xiàn) Web Service,或者將 Java 接口標(biāo)記為定義 Web Service 接口
WebMethod定制Web Service方法
WebParam定制Web Service方法的參數(shù)
WebResult定制Web Service方法的返回值
SOAPBinding指定WebService的SOAP映射樣式
使用標(biāo)注可以在不改變代碼邏輯的前提下讓外部代碼能夠獲得更多的元數(shù)據(jù)。下面就用javax.jws定義的標(biāo)注來聲明一個(gè)WebServicepom增加CXF依賴     <dependencies>           <dependency>                <groupId>org.apache.cxf</groupId>                <artifactId>apache-cxf</artifactId>                <version>${cxf.version}</version>                <type>pom</type>           </dependency>     </dependencies>     <properties>           <cxf.version>2.2.4</cxf.version>     </properties>增加接口服務(wù)packagecom.dogiant.demo;importjavax.jws.WebService;@WebServicepublicinterfaceCXFDemo {     publicString sayHello(Stringfoo);}實(shí)現(xiàn)服務(wù)類packagecom.dogiant.demo;importjavax.jws.WebService;@WebService(endpointInterface ="com.dogiant.demo.CXFDemo", serviceName ="cxfDemo")publicclassCXFDemoImplimplementsCXFDemo {     @Override     publicString sayHello(Stringfoo) {           return"hello "+foo;     }}三、以endpoint發(fā)布

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

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

下面的測(cè)試類包含了發(fā)布服務(wù)和客戶端調(diào)用的代碼:           <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>4.12</version>                <scope>test</scope>           </dependency>packagecom.dogiant.demo;importjavax.xml.ws.Endpoint;importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;importorg.junit.Assert;importjunit.framework.TestCase;publicclassTestEndpointextendsTestCase {          privatestaticfinalStringADDRESS= "http://localhost:9000/cxfdemo" ;     protectedvoidsetUp()throwsException {           super.setUp();           System.out.println("Starting Server");           CXFDemoImpldemo= new CXFDemoImpl();           Endpoint.publish(ADDRESS,demo);           System.out.println("Start success");     }     publicvoidtestSayHello() {           JaxWsProxyFactoryBeanfactory= new JaxWsProxyFactoryBean();           factory.setServiceClass(CXFDemo.class);           factory.setAddress(ADDRESS);           CXFDemoclient= (CXFDemo)factory.create();           Assert.assertEquals(client.sayHello("foo"),"hello foo");     }}信息: Creating Service {http://demo.dogiant.com/}CXFDemoImplService from class com.dogiant.demo.CXFDemo2016-3-28 10:49:48 org.apache.cxf.endpoint.ServerImpl initDestination信息: Setting the server's publish address to be http://localhost:9000/cxfdemo2016-3-28 10:49:48 org.mortbay.log.Slf4jLog info信息: Logging to org.slf4j.impl.JDK14LoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog2016-3-28 10:49:48 org.mortbay.log.Slf4jLog info信息: jetty-6.1.212016-3-28 10:49:48 org.mortbay.log.Slf4jLog info信息: Started SelectChannelConnector@0.0.0.0:9000Start success2016-3-28 10:49:48 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://demo.dogiant.com/}CXFDemoService from class com.dogiant.demo.CXFDemo四、在webapp中發(fā)布web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "     version="2.5">     <display-name>spring-cxf-demo</display-name>          <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>classpath*:spring/spring-config*.xml </param-value>     </context-param>          <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>          <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-app>spring-config-cxf.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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">     <importresource="classpath:META-INF/cxf/cxf.xml"/>     <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>     <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>     <jaxws:endpointid="cxfDemo"implementor="com.dogiant.demo.CXFDemoImpl"           address="/cxfdemo"/></beans>http://localhost:8080/services/cxfdemo           <dependency>                <groupId>javax.servlet</groupId>                <artifactId>servlet-api</artifactId>                <version>2.5</version>                <scope>provided</scope>   //此處不注意會(huì)報(bào)錯(cuò)           </dependency>http://localhost:8080/services/cxfdemo?wsdl五、客戶端packagecom.dogiant.demo;importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;publicclassCXFClient {     publicstaticvoidmain(String[]args) {      JaxWsProxyFactoryBeanproxy= new JaxWsProxyFactoryBean();           proxy.setServiceClass(CXFDemo.class);           proxy.setAddress("http://localhost:8080/services/cxfdemo");           CXFDemocxf= (CXFDemo)proxy.create();           System.out.println(cxf.sayHello("haha"));     }}與spring集成<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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">     <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">           <propertyname="serviceClass"value="com.dogiant.demo.CXFDemo"/>           <propertyname="address"value="http://localhost:8080/services/cxfdemo"/>     </bean>     <beanid="client"class="com.dogiant.demo.CXFDemo"factory-bean="clientFactory"           factory-method="create"/></beans>測(cè)試用例packagecom.dogiant.demo;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:spring/spring-config-cxf-client.xml"})publicclassTestCXFClient {          @Autowired     privateCXFDemoclient;          @Test     publicvoidtest() {           System.out.println(client.sayHello("hello"));     }}

附:

cxf官網(wǎng) February 8, 2016 - Apache CXF 3.1.5/3.0.8 released!http://cxf.apache.org/index.html

cxf-demo例子

https://github.com/dogiant/cxf-demo 

cxf官網(wǎng) February 8, 2016 - Apache CXF 3.1.5/3.0.8 released!http://cxf.apache.org/index.html
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江津市| 中西区| 板桥市| 建平县| 通河县| 临泽县| 海宁市| 梅河口市| 安塞县| 开鲁县| 海盐县| 曲周县| 长春市| 积石山| 永善县| 云梦县| 宣城市| 玉溪市| 车险| 柳江县| 姜堰市| 昌吉市| 阳山县| 汽车| 吉木乃县| 佳木斯市| 小金县| 中超| 积石山| 顺平县| 大连市| 讷河市| 朝阳县| 甘谷县| 修文县| 三亚市| 洛南县| 萍乡市| 弥渡县| 屯留县| 安龙县|