分分钟带你玩转 Web Services【2】CXF
在实践中一直在使用 JAX-WS 构建 WebService 服务,服务还是非常稳定、高效的。
但还是比较好奇其他的 WebService 开源框架,比如:CXF/Axis2/Spring WS等。
源于对 Apache 的信赖和喜爱, 旗下的 CXF WebService 肯定也不会让人失望。
所以花了点时间将 CXF 引入到项目实践当中,多一种选择也未尝不可。
对于 WebService 和 CXF 简介这里就不赘述了,不太懂的同学请先移步:分分钟带你玩转 Web Services【1】JAX-WS
本篇试从 Servlet 发布 CXF WebService 和 Spring 托管 CXF WebService 两种方式,带你玩转 CXF。
Servlet 发布 CXF WebService git demo地址:http://git.oschina.net/LanboEx/cxf-demo
Spring 托管 CXF WebService git demo地址: http://git.oschina.net/LanboEx/cxf-spring-demo
需要有这方面实践的同学,请收藏这篇博客,到时只需将 Demo 在本地跑起来,一切就都明朗了。
官网地址(如果你对你英文很自信的话):http://cxf.apache.org/docs/index.html
1. Servlet 发布 CXF WebService
a.mavn 依赖 Jar:
<!--web 容器支持-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency> <!--apache cxf webservice-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
依赖的 Jar 基本上都是 sum/apache/codehaus 支持,这些 Jar 已经过岁月洗涤,稳定高效。
b.服务实现:
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface UserService { /**
* 执行测试的WebService方法(有参)
*/
@WebMethod
String sayHi(@WebParam(name = "name") String name);
}
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class UserServiceImpl implements UserService { @WebMethod
public String sayHi(String name) {
return "Hi, " + name + "! ";
}
}
c.Servlet 实现:
public class WebServicesServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = -5314312869027558456L; @Override
protected void loadBus(ServletConfig servletConfig) {
super.loadBus(servletConfig);
Endpoint.publish("/UserService", new UserServiceImpl());
}
}
你没有看错 CXF 提供的不集成 Spring 的 Servlet 就叫做 CXFNonSpringServlet,是不是有点俗。
使用起来也很简单,实现 org.apache.cxf.transport.servlet.CXFNonSpringServlet 中的 loadBus 方法即可。
d.web.xml 配置:
<servlet>
<servlet-name>cxfwsServlet</servlet-name>
<servlet-class>com.rambo.cxf.demo.ws.servlet.WebServicesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfwsServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
启动工程,访问:http://localhost:4042/cxf-demo/ws/UserService?wsdl
Servlet 发布 CXF WebService 方式在后续推广使用中发现,部署 weblogic 时会出现错误:
java.lang.IllegalArgumentException: Cannot create URL for this address /XXXXXX
而该错误需要降低 CXF 版本才能顺利解决,这已经违背了我们项目开发的原则。
参考地址:http://git.net/ml/users-cxf-apache/2014-07/msg00027.html
2. Spring 托管 CXF WebService
a.mavn 依赖 Jar:
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <!--apache cxf webservice-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
只需要依赖 SpringWeb 即可,其中 Spring Context 来托管 WebService 服务的实现类。
服务实现和 web.xml 与 Servlet 发布 是一致的,不需要进行特殊的处理,这里就不贴了。
b. 将 WebService 服务托管给 Spring Context
<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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="userService" class="com.rambo.cxf.spring.demo.impl.UserServiceImpl"/> <jaxws:endpoint id="userServiceWs" implementor="#userService" address="/UserService"/>
</beans>
启动工程,访问:http://localhost:4047/cxf-spring-demo/ws/UserService?wsdl
3. 小结
Servlet 发布 CXF WebService 依赖的开源库较少,也就是说出问题的概率较小;
每次新增 WebService 服务需要修改 WebServicesServlet 类。
Spring Context 托管 WebService 实现类,新增服务类可配置在 cxf-servlet.xml 中;
有优秀的容器替你管理,你会很舒服,大型复杂 WebService 建议配合 Spring 使用。
4. CXF 客户端
当然客户端的使用也有两种方式:
a. 使用工具或者命令行,根据 wsdl 文档生成调用代码,进行调用访问(在上篇中描述过);
b. 配合 spring 进行配置,无需生成客户端代码,交由 spring 统一管理;
<!-- 方式一:基于spring的jaxws -->
<jaxws:client id="UserService"
serviceClass="com.rambo.cxf.spring.demo.ws.inter.UserService"
address="http://localhost:4047/cxf-spring-demo/ws/UserService">
</jaxws:client> <!-- 方式二:基于cxf的JaxWsProxyFactoryBean -->
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.rambo.cxf.spring.demo.inter.UserService"/>
<property name="address" value="http://localhost:4047/cxf-spring-demo/ws/UserService"/>
</bean>
a 方式被调用方项目不需要添加任何依赖,即可使用客户端代码进行调用访问。
但当服务中涉及交互的安全控制时,还是需要将所依赖添加至项目中,所依赖的 jar 相当庞大。
涉及安全认证时使用 spring 方式配置是最妥当的,引入服务的接口层和相关的依赖即可。
分分钟带你玩转 Web Services【2】CXF的更多相关文章
- 分分钟带你玩转 Web Services
当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来. 毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放. 怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防 ...
- 分分钟带你玩转 Web Services【1】JAX-WS
当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来. 毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放. 怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防 ...
- 老司机带你玩转web service
当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来.毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放.怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防火墙 ...
- 带你玩转Visual Studio
带你玩转Visual Studio 带你新建一个工程 工程目录下各文件的含义 解决方案与工程 在这之前先了解一个概念:解决方案与工程. 解决方案(Solution):一个大型项目的整体的工作环境: 工 ...
- RESTful Web Services初探
RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...
- Jersey the RESTful Web Services in Java
Jersey 是一个JAX-RS的实现, JAX-RS即Java API for RESTful Web Services, 支持按照表述性状态转移(REST)架构风格创建Web服务. REST 中最 ...
- (转) Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么?
Web 建站技术中,HTML.HTML5.XHTML.CSS.SQL.JavaScript.PHP.ASP.NET.Web Services 是什么? 建站有很多技术,如 HTML.HTML5.XHT ...
- 利用WSCF进行契约先行的Web Services开发
http://www.cnblogs.com/goody9807/archive/2007/06/05/772107.html 什么是契约先行(Contract-First)? 如果说一个新的软件开发 ...
- RESTful Web Services测试工具推荐
命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...
随机推荐
- HAproxy健康检查的三种方式
1.通过监听端口进行健康检测 .这种检测方式,haproxy只会去检查后端server的端口,并不能保证服务的真正可用. 配置示例: listen http_proxy mode http cooki ...
- 1432: [ZJOI2009]Function
1432: [ZJOI2009]Function Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 710 Solved: 528[Submit][Stat ...
- 3018: [Usaco2012 Nov]Distant Pastures
3018: [Usaco2012 Nov]Distant Pastures Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: 20[ ...
- 深度剖析Spark分布式执行原理
让代码分布式运行是所有分布式计算框架需要解决的最基本的问题. Spark是大数据领域中相当火热的计算框架,在大数据分析领域有一统江湖的趋势,网上对于Spark源码分析的文章有很多,但是介绍Spark如 ...
- Timestamp解析0000-00-00 00:00:00报格式错误
mysql中存储的是Timestamp类型的0000-00-00 00:00:00, 但是在java程序中使用 Timestamp.valueOf("0000-00-00 00:00:00& ...
- 移植python笔记
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 编译环境:ubuntu-14.04.1 编译器:gcc.arm-hisiv200-linux-gnueabi P ...
- 使用Topshelf组件构建简单的Windows服务
很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解 ...
- Android中查看布局文件中的控件(view,id)在哪里被调用(使用)
在阅读别人的代码时通常是很痛苦的,有时很想要看一看布局中的控件在哪里被调用了,为之很苦恼 在这里提供一种方法. 复制要查看的控件ID,到R文件中搜索到该ID, 接下来就好办的了,选中ID按下C ...
- CSS.01 -- 选择器及相关的属性文本、文字、字体、颜色、
与html相比,Css支持更丰富的文档外观,Css可以为任何元素的文本和背景设置颜色:允许在任何元素外围设置边框:允许改变文本的大小,装饰(如下划线),间隔,甚至可以确定是否显示文本. 什么是CSS? ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...