一、對比Axis2和CXF
jws的發布對java webservice框架產生了巨大的影響,經過大浪淘沙,目前java開發webservice的框架主要包括axis2和cxf。
axis2和cxf都是apache旗下的產品,但是其目的不同,導致webservice開發方法也不一樣。兩個框架都得到了開發者的支持。有必要對二者進行以下對比。
Axis2 | CXF | |
---|---|---|
目標 | WebService引擎 | 簡易的SOA框架,可以作為ESB |
ws* 標準支持 | 不支持WS-Policy | WS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic PRofile |
數據綁定支持 | xmlBeans、JiBX、JaxMe 、JaxBRI、ADB | JAXB, Aegis, XMLBeans, SDO, JiBX |
spring集成 | 不支持 | 支持 |
應用集成 | 困難 | 簡單 |
多語言 | 支持C/C++ | 不支持 |
部署 | web應用 | 嵌入式 |
服務監控和管理 | 支持 | 不支持 |
結論:
如果希望以一種一致的方式實現webservice,特別是有跨語言的需求時,應該使用Axis2如果需要在現有的java程序(包括web應用)中增加webservice支持,應該使用CXF從Java6開始,WebService API從Java EE復制到了Java SE。并遵循了一系列的標準,比如JSR181(Web Service 元數據),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件標準)等。 并分別定義到javax.jws, javax.xml.ws 和 javax.xml.soap包中。
JSR181支持使用標注(annotation)來定義WebService。在javax.jws中主要的標注類包括:
標注 | 說明 |
---|---|
WebService | 將 Java 類標記為實現 Web Service,或者將 Java 接口標記為定義 Web Service 接口 |
WebMethod | 定制Web Service方法 |
WebParam | 定制Web Service方法的參數 |
WebResult | 定制Web Service方法的返回值 |
SOAPBinding | 指定WebService的SOAP映射樣式 |
到目前為止,使用的都是標準Java SE中的東西。下面要開始依賴CXF實現一些功能。
首先是服務的發布。CXF不僅支持通過Web容器發布WebService,也可以在嵌入式代碼中通過jetty發布WebService。
下面的測試類包含了發布服務和客戶端調用的代碼: <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中發布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> //此處不注意會報錯 </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>測試用例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官網 February 8, 2016 - Apache CXF 3.1.5/3.0.8 released!http://cxf.apache.org/index.htmlcxf-demo例子
https://github.com/dogiant/cxf-demo
cxf官網 February 8, 2016 - Apache CXF 3.1.5/3.0.8 released!http://cxf.apache.org/index.html新聞熱點
疑難解答