使用apache CXF和maven開發Web Service

目前主要的java webservice框架剩下了axis2和cxf。本文對兩個框架的目標、標準支持、開發和部署等方面進行了簡單的對比。對於在現有web應用中發佈webservice,本文建議使用cxf。 更進一步,本文介紹了cxf的嵌入式代碼和web容器兩種發佈方式。

本文中的例子使用maven進行構建。

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應用 嵌入式
服務監控和管理 支持 不支持

結論:

  1. 如果希望以一種一致的方式實現webservice,特別是有跨語言的需求時,應該使用Axis2
  2. 如果需要在現有的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,如下圖:

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

Author: Holbrook

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章