本文主要介绍了,如何使用cxf内置的例子,学会开发webserivce,在web项目中使用,且包含spring支持。

webserivce的开发可以使用cxf或者axis,好像还有httpclient等等。以前也多次研究过,上网搜过很多别人的例子来看,写过代码下来,但没有总结过,少废话,上干货。

1. 到cxf的官网下载jar包。我用的是以前下载下来的apache-cxf-2.7.6.zip,并非最新版本。下载完成后,解压后,目录结构如下左图:

打开其中的samples文件夹,其内包含了很多例子的代码,找到我们要使用的例子java_first_spring_support,导入到Myeclipse中。

2. 导入之后,Maven下载完项目依赖的jar包后,我们可以看到,项目里面已经写好web.xml文件,cxf-servlet.xml文件和client-beans.xml文件。client-beans.xml文件,是客户端调用时要用到的。ws主要加载的是cxf-servlet.xml文件。为了项目启动时加载它,修改已经写好的web.xml文件内容,在其中加入以下代码:

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/cxf-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

注意,cxf-servlet.xml文件原来是在WEB-INF目录下的,写法/WEB-INF/classes/cxf-servlet.xml要求项目编译完成后,cxf-servlet.xml文件要在classes下面找得到,

所以,要把该文件配置在了classpath下面,如下图:

3. 完成相应的修改和配置后,启动项目,如控制台不报错,可以看到相应webservice创建的信息,如下:

2015-7-24 17:56:16 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19a8739b: defining beans [cxf,org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor,org.apache.cxf.bus.spring.Jsr250BeanPostProcessor,org.apache.cxf.bus.spring.BusExtensionPostProcessor,helloWorld]; root of factory hierarchy
2015-7-24 17:56:17 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.spring.demo/}HelloWorldImplService from class demo.spring.service.HelloWorld
2015-7-24 17:56:18 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /HelloWorld
2015-7-24 17:56:18 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization completed in 2671 ms

4. 客户端调用。项目中包含了调用的测试代码,无须我们手动再编写,直接运行Client.java即可。

 值得注意的是,Client.java并不直接包含客户端调用逻辑的代码,而是通过调用一个cxf的工厂类,显然逻辑是在该工厂类内定义的,这个我们不必关心,只负责调用即可。

但,工厂类的一个属性address的值要修改一下,

即配置文件中client-beans.xml的<property name="address" value="http://localhost:9002/services/HelloWorld"/>的value,

本人的地址是,http://localhost:8080/JavaFirstSpringSupport/services/HelloWorld

至此,开发完成。

在web项目中使用cxf开发webservice,包含spring支持的更多相关文章

  1. 【WebService】使用CXF开发WebService(四)

    CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...

  2. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  3. CXF 开发 WebService

    什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...

  4. 使用cxf开发webservice接口

    项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...

  5. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  6. 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例

    通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合 1. 在前面的 父工程 ws_parent 中 添加依赖 由于原来的项目是使用的cxf依赖 ...

  7. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

  8. JAVA WEB项目中各种路径的获取

    JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在s ...

  9. 使用cxf开发webservice应用时抛出异常

    在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...

随机推荐

  1. boost log库

    http://blog.csdn.net/sheismylife/article/category/1820481

  2. lepus监控OS配置

    Lepus通过snmp协议进行对操作系统数据采集,因此需要在监控机和被监控机开启snmp服务 snmp协议:简单网络管理协议(SNMP,Simple Network Management Protoc ...

  3. sqlserver 按日、周、月统计方法

    摘自于网络网络:http://blog.csdn.net/wanmdb/article/details/8080636 create table T(日期时间 datetime, 数量 int) in ...

  4. 使用非 GUI 模式运行 JMeter 压力测试

    使用非 GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源.使用命令jmeter -n -t <testplan filename> -l <list ...

  5. SpringMVC 学习-入门篇

    一.都需要哪些 Jar 包 <dependencies> <dependency> <groupId>org.springframework</groupId ...

  6. spring中的控制反转IoC和依赖注入DI

    原文:http://blog.163.com/xianghuxian@126/blog/static/50639037200721345218382/ IoC(Inversion of Control ...

  7. git 常用使用及问题记录

    1.打开bash,进入工程根目录(引用whaon的话:是和.classpath和.project同级的目录).PS:我的系统是win7,在bash切换到E的命令是 cd /e: 2.运行 git in ...

  8. HttpWatch工具简介及使用技巧(转载)

    一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...

  9. Web开发人员不要错过的60款用户界面设计工具(上)

    Web开发大师们,干货再次来袭!小编为大家盘点了60款功能丰富类型各异的用户界面设计工具,本系列将以上中下三篇分别为大家呈现.今天盘点的这20款工具囊括了大量界面原型设计工具,有免费的在线原型工具,有 ...

  10. hdu_5753_Permutation Bo(找规律)

    题目连接:hdu_5753_Permutation Bo 题意: 给你一个有n个数的数列c1~cn,h1~hn为1~n的排列,求ci[hi>hi-1  and  hi>hi+1]的期望和. ...