【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,尝试过直 ...
随机推荐
- 你不知道的javaScript笔记(2)
this和对象原型 this是一个很特别的关键字,被自动定义在所有函数的作用域中 // foo.count 是0,字面理解是错误的 function foo(num) { console.log(&q ...
- 取消Eclipse的自动代码格式化
前段时间在Eclipse里面设置了java文件保存时自动格式化,在java->Code Style->Formatter里设置了自定义的格式化的样式,这样每次保存后都会自动格式化代码,用了 ...
- bzoj2982: combination(lucas)
Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案 ...
- vue入门——组件基础todolist
1. 以下是 todolist 的例子,没有用到组件:下面的3 会通过组件拆分todolist <!DOCTYPE html> <html lang="en"&g ...
- Element-ui树形控件el-tree获取父级节点的id
Element-ui官网给的方法 getCheckedKeys() { console.log(this.$refs.tree.getCheckedKeys()); }, 这种只有在所有子级都被选中的 ...
- Java处理中文乱码问题
package servlet; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.ser ...
- Python的scrapy之爬取豆瓣影评和排名
基于scrapy框架的爬影评 爬虫主程序: import scrapy from ..items import DoubanmovieItem class MoviespiderSpider(scra ...
- linux 热替换so文件
http://www.zhaoch.top/操作系统/linux/热替换so文件.html 热替换so文件 www.zhaoch.top > 操作系统 > linux 发现nginx的动态 ...
- JavaSE基础复习---1---2018/9/27
2018/9/27 JavaSE学习笔记-1 目录: Java的起源 Java语言概述 1.Java的起源 现代编程语言的发展,大致可以理解为,机器码语言---汇编语言---C语言---C++语言-- ...
- Electron入门应用打包exe(windows)
最近在学习nodejs,得知Electron是通过将Chromium和Node.js合并到同一个运行时环境中,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一门技术.对于之前一直从 ...