【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,尝试过直 ...
随机推荐
- C/C++使用Socket通信UDP
接收端 #include <stdio.h> #include <WinSock2.h> #pragma comment(lib,"WS2_32.lib") ...
- 在Windows系统上使用压缩归档文件安装MySQL流程
最近需要做个小小的验证实验,需要安装MySQL,网上一搜发现教程繁多,bug也多,所以直接把官网的流程翻译过来,注意是压缩文件,不是安装版的,解压直接能用的,下面直接把流程贴过来: 使用压缩文档安装在 ...
- MyBatis模糊查询的三种拼接方式
1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%'); 2. 使用 ${...} ...
- lvs初体验
一.简介 LVS是 Linux Virtual Server 的简称,也就是Linux虚拟服务器.这是一个由章文嵩博士发起的一个开源项目,它的官方网址是http://www.linuxvirtuals ...
- 新知识 HtMl 5
快要毕业了,即将走向实习岗位,但是这日子过的太无聊了,昨天逃课回宿舍打开电脑想看电影但是没什么好看的,于是上床睡觉了,越躺越无聊,然后爬了起来到学习图书馆找了本HTML5的课本,学习了起来(我感觉ht ...
- dom4j里面封装方法的操作
animal.xml <?xml version="1.0" encoding="UTF-8"?><animal> <cat ...
- python 中 pynlpir错误 Cannot Open Configure file pynlpir\Data\Configure.xml 解决
在用python做分词.数据处理的时候,想调用pynlpir库,pynlpir.open()时出现错误,更新一下授权文件还是错误, 仔细一看错误是:Cannot Open Configure file ...
- re模块(详解正则)
re模块 imort re 1.\w \W print(re.findall('\w','ab 12\+- _*&')) #\w 匹配字母 数字 及下划线 执行结果:['a', 'b', '1 ...
- 017---Django的中间件解决跨域
跨域 跨域是什么? 浏览器从一个域名的网页去请求另一个域名的资源的时候,如果不同源.请求的响应结果就会被浏览器的同源策略所拦截 同源策略是什么? 同源:协议 + 域名 + 端口 特点:阻止ajax请求 ...
- phpMyAdmin出现错误 Access denied for user 'root'@'localhost' (using password: NO)
今天安装wmpp,之后启动后点击phpMyAdmin 报拒绝连接错误:#1045 - Access denied for user 'root'@'localhost' (using password ...