使用apache CXF和maven开发Web Service

来源:互联网 发布:剑三藏剑二少捏脸数据 编辑:程序博客网 时间:2024/06/06 10:49

目前主要的java webservice框架剩下了axis2和cxf。本文对两个框架的目标、标准支持、开发和部署等方面进行了简单的对比。对于在现有web应用中发布webservice,本文建议使用cxf。 更进一步,本文介绍了cxf的嵌入式代码和web容器两种发布方式。

本文中的例子使用maven进行构建。

Table of Contents

  • 1 对比Axis2和CXF
  • 2 编写服务类
  • 3 以endpoint发布
  • 4 在webapp中发布

1 对比Axis2和CXF

jws的发布对java webservice框架产生了巨大的影响,经过大浪淘沙,目前java开发webservice的框架主要包括axis2和cxf。

axis2和cxf都是apache旗下的产品,但是其目的不同,导致webservice开发方法也不一样。两个框架都得到了开发者的支持。有必要对二者进行以下对比。

  Axis2CXF目标WebService引擎简易的SOA框架,可以作为ESBws* 标准支持不支持WS-PolicyWS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic Profile数据绑定支持XMLBeans、JiBX、JaxMe 、JaxBRI、ADBJAXB, Aegis, XMLBeans, SDO, JiBXspring集成不支持支持应用集成困难简单多语言支持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;@WebServicepublic 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.TestEndpointStarting Server2012-12-12 11:29:02 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass??Ϣ: Creating Service {http://cxfdemo/}CXFDemoImplService from class cxfdemo.CXFDemo2012-12-12 11:29:03 org.apache.cxf.endpoint.ServerImpl initDestination??Ϣ: Setting the server's publish address to be http://localhost:9000/cxfdemo2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info??Ϣ: jetty-7.4.2.v201105262012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info??Ϣ: Started SelectChannelConnector@localhost:9000 STARTING2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info??Ϣ: started o.e.j.s.h.ContextHandler{,null}Start success2012-12-12 11:29:04 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass??Ϣ: Creating Service {http://cxfdemo/}CXFDemoService from class cxfdemo.CXFDemoTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.076 secResults :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

 

0 0