【WebService】——CXF整合Spring
相关博客:
前言:
之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架的东西。这样的操作对WebService客户端、服务端的调用、生成等关系的理解会比较深刻,当然也有很大的弊端,需要手动拷贝生成的代码,进行大量重复性的工作。这时候就需要引入框架了,本篇博客介绍其中一种框架CXF,他可以和spring整合实现WebService。
1、下载CXF
apache-cxf-2.5.0
解压之后可以看到它的目录结构,其中bin中主要是一些bat文件或命令,后面会经常用到的就是wsdl2java。lib中是一系列的jar包。因为cxf支持spring,自带的lib中有一些spring的核心包。
2、环境变量
在Path的值后加“;E:\software\apache-cxf-2.5.0\bin” 注意:加上;分号
测试是否配置成功:
cd E:\software\apache-cxf-2.5.0\bin
e:
wsdl2java
如果出现如下信息,则说明已经可以找到该命令,环境变量配置成功。
3、新建项目(服务端)
引入jar包:
将cxf解压文件的lib目录下的jar包加到项目中。
与前几篇博客相同,我们在服务端给出一个接口IHelloWorld和实现类HelloWorldImpl。
IHelloWorld接口的方法为:
@WebService
public interface IHelloWorld {
public String sayHello(String text);
}
实现类HelloWorldImpl。
@WebService(endpointInterface="cn.com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
public String sayHello(String text) {
return "Hello" + text ;
}
}
配置文件
1)applicationContext.xml
在src下新建该文件,其中import标签引入的三个配置文件在cxf的核心jar包下,具体路径:cxf-2.5.0.jar\META-INF\cxf。
<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="hello" class="cn.com.service.HelloWorldImpl"/> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> </beans>
2)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 该listener保证 在web应用启动时加载spring容器 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--所有来自/*的请求,都交由CXFServlet处理 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<!-- <display-name>CXF Servlet</display-name> -->
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
发布服务
将项目部署到tomcat上,启动后在浏览器地址输入http://localhost:8080/webws/HelloWorld?wsdl (webws为项目名称)。
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://service.com.cn/">
<wsdl:portType name="HelloWorldImpl"></wsdl:portType>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorldImpl">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/webws/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
4、生成客户端
新建一个空的客户端项目,进入src目录,使用wsdl2java生成客户端。
Refresh刷新项目,可以看到,客户端项目中生成的相应的文件,效果和使用wsimport命令相同。
测试:
在客户端新建测试类,注意在测试的时候要保证tomcat上部署好服务。
public static void main(String[] args) {
HelloWorldImplService factory=new HelloWorldImplService();
IHelloWorld hw=factory.getHelloWorldImplPort();
System.out.println(hw.sayHello("Sherry"));
}
以上就是cxf整合spring实现WebService调用的例子。小编认为,cxf框架实现WebService的亮点有两个:一是将生成客户端的命令进行了封装,省去了用jdk开发时复杂的命令输入和繁琐的文件拷贝工作。二是整合spring,spring框架的优点就不用多说了,大大提高了开发的效率和可扩展性。
【WebService】——CXF整合Spring的更多相关文章
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成
首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...
- CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...
- CXF整合Spring之JaxWsProxyFactoryBean调用
1.见解 1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活 1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理 ...
- cxf整合spring错误为:cvc-complex-type.2.4.c
cxf整合spring,报错信息如下: Multiple annotations found at this line:- cvc-complex-type.2.4.c: The matching w ...
- cxf整合spring中出现的错误
Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...
- CXF整合Spring发布WebService实例
一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...
- CXF整合spring
近公司需要弄webservics,还说不用框架整合(提倡使用hessian,他们既然说与操作系统有兼容问题,由于人员单薄,不得不屈服,哎),我想了老半天没弄明白他说的不用框架整合spring,尝试过直 ...
随机推荐
- iOS之某公司iOS开发笔试题
参考答案不唯一,大家可以根据自己的理解回答,没有必要跟笔者的一样.参考笔者的答案,也许给你带来灵感! 1.对数组中的元素去重复 例如: NSArray *array = @[@"12-11& ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- intellij IEDA 从svn拉环境到正常运行
intellij IEDA 从svn拉环境到正常运行 1.svn拉项目 在项目选择界面点击Check out from Version Control 从中选择Subversion(SVN) 2.选 ...
- python 方法解析顺序 mro
一.概要: mor(Method Resolution Order),即方法解析顺序,是python中用于处理二义性问题的算法 二义性: 1.两个基类,A和B都定义了f()方法,c继承A和B那么C调用 ...
- Spring笔记2
Bean生命周期 1 实例化 2 注入属性 3 BeanNameAware 4 BeanFactoryAware 5 ApplicationContextAware 6 BeanPostProcess ...
- tp5多条件查询
->where('m.user_nickname|w.nickname|c.companyname','like','%'.$search.'%')\
- Linux编译移植Qt5的环境_OMAPL138平台
Linux编译移植Qt5_OMAPL138 [导语]:昨天编译Qt5各种失败,各种离奇的错误在Google上面也搜索不到,真是让人"蛋疼菊紧",今天把所有的环境全部清理干净,也重新 ...
- 对bluebird的理解
前言 Promise:把原来的回调写法分离出来,在异步操作执行完后,用链式调用的方式执行回调函数. 在公众号的开发里面用的const Promise = require('bluebird');con ...
- ajax设置自定义头
一.setting参数 headers $.ajax({ headers: { Accept: "application/json; charset=utf-8" ...
- Spring+Hiberate 多数据源的网文整理
解决方案: http://www.th7.cn/Program/java/2011/10/23/44664.shtml 分析共享Spring配置数据源四种方式(附相应jar包) :http://ww ...