分分钟带你玩转 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 ...
随机推荐
- C++ 容器对象vector和list 的使用
在<<c++ primer>>第四版Exercise Section 9.3.4 的Exercise 9.20 是这样的一道题目:编写程序判断一个vector<int&g ...
- Firefox52非HTTPS页面登录页面提示连接不安全的解决办法
背景: Firefox52版本开始,对于非HTTPS协议的登录页面,会提示链接不安全,如下图 解决办法很简单,上HTTPS协议(严重推荐,尤其是祖国这种特殊国情下,上HTTPS的协议好处多多,物超所值 ...
- C#(.Net)知识点记录
一:批量插入"SqlBulkCopy"的应用 PS:"SqlBulkCopy"的官方释义:"Lets you efficiently bulk loa ...
- spring切面编程AOP 范例一
参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...
- mysql编程--创建函数出错的解决方案
本文章转载自:http://www.jb51.net/article/71100.htm 在使用MySQL数据库时,有时会遇到MySQL函数不能创建的情况.下面就教您一个解决MySQL函数不能创建问题 ...
- [译]Selenium Python文档:三、导航控制
你使用WebDriver要做的第一件事就是访问一个链接.一般通过调用get方法来实现: driver.get("http://www.baidu.com") 在将控制权返给你的脚本 ...
- Hadoop2.7.3+Spark2.1.0完全分布式集群搭建过程
1.选取三台服务器(CentOS系统64位) 114.55.246.88 主节点 114.55.246.77 从节点 114.55.246.93 从节点 之后的操作如果是用普通用户操作的话也必须知道r ...
- Alamofire源码解读系列(九)之响应封装(Response)
本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...
- node.js平台下,利用cookie实现记住密码登陆(Express+Ejs+Mysql)
本博文需有node.js+express+mysql入门基础,若基础薄弱,可参考博主的其他几篇node.就是博文: 1.下载Mysql数据库,安装并配置 创建用户表供登录使用: 2.node.js平台 ...
- 解决Appium 抓取toast
首先我们先看看这个gif,图中需要,要抓取的字符串--->请输入转让份数 1.要导入java-client-5.0.0-SNAPSHOT.jar 包的地址:链接:http://pan.baidu ...