目前主要的java webservice框架剩下了axis2和cxf。本文對兩個框架的目標、標准支持、開發和部署等方面進行了簡單的對比。對於在現有web應用中發布webservice,本文建議使用cxf。 更進一步,本文介紹了cxf的嵌入式代碼和web容器兩種發布方式。
本文中的例子使用maven進行構建。
Table of Contents
1 對比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
2 編寫服務類
從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映射樣式 |
使用標注可以在不改變代碼邏輯的前提下讓外部代碼能夠獲得更多的元數據。下面就用javax.jws定義的標注來聲明一個WebService:
- 創建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>
- 創建服務接口
package cxfdemo; import javax.jws.WebService; @WebService public interface CXFDemo { public String sayHello(String foo); }
- 實現服務類
package cxfdemo; import javax.jws.WebService; @WebService() public class CXFDemoImpl implements CXFDemo { public String sayHello(String foo) { return "hello "+foo; } }
3 以endpoint發布
到目前為止,使用的都是標准Java SE中的東西。下面要開始依賴CXF實現一些功能。
首先是服務的發布。CXF不僅支持通過Web容器發布WebService,也可以在嵌入式代碼中通過jetty發布WebService。
下面的測試類包含了發布服務和客戶端調用的代碼:
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 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ... ...
4 在webapp中發布
CXF提供了spring的集成,同時還提供了org.apache.cxf.transport.servlet.CXFServlet用於在web容器中發布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配置文件內容如下
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容器中發布了。啟動web應用:
$mvn jetty:run
就可以在瀏覽器中看到已經發布的WebService,如下圖: