使用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开发方法也不一样。两个框架都得到了开发者的支持。有必要对二者进行以下对比。
结论:
- 如果希望以一种一致的方式实现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中主要的标注类包括:
使用标注可以在不改变代码逻辑的前提下让外部代码能够获得更多的元数据。下面就用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
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- Java使用Apache CXF开发Web Service
- 使用Apache CXF开发Web Service步骤
- 使用Apache CXF和Spring集成创建Web Service
- 使用Apache CXF和Spring集成创建Web Service
- 使用Apache CXF和Spring集成创建Web Service
- Apache CXF开发Web Service 目录
- 使用Apache CXF创建Web Service
- 使用Apache CXF创建简单Web Service
- 使用 Apache CXF 实现 Web Service 详解
- 使用Apache CXF实现Web Service
- 使用 Apache CXF 实现 Web Service 详解
- 外挂辅助技术分析任务对象基址及封装任务更新函数
- 用U盘启动vmare虚拟机
- 一个有点意思的SQL问题(连续的序列并且值相同的用区间来体现)
- 状态模式的应用
- Android-性能优化典范(第一季)
- 使用apache CXF和maven开发Web Service
- 使用php+session实现登陆
- Lua 的协程 为什么会出现?
- C# 划分多空格
- 从排序开始---算法之旅
- hadoop-1.2.1集群配置搭建
- 海外优秀资讯抢先看5:私有应用云平台Sandstorm服务对象之个人用户
- 正则表达式 —— 插入符(anchor, ^)和美元符($)
- 自定义四个方向上的SlidingDrawer(抽屉效果)