从网上看了很多CXF的资料,大部分都是单独的作为一个webservice项目,对于在现有的spring项目上提供webservice服务的例子基本没有找到。

我做的这个例子是介绍怎么把cxf整合到现有的spring项目中,现在只做到可以传简单的字符串和JAVABEAN,复杂的以后研究。

      这是例子的下载地址:一个简单的CXF例子

     一,应用cxf应该先把该服务所需要的架包加载进项目中。

对于一个已经搭建好的spring项目,我做的项目中所缺少的架包是

cxf-2.4.3.jar   ,   neethi-3.0.1.jar     ,     wsdl4j-1.6.2.jar     ,   xmlschema-core-2.0.1.jar    ,commons-logging-1.1.1.jar   ,spring一系列的架包

二,首先是服务接口

  1. package com.zcz.cxf.service;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface GreetingService {
  5. public String greeting(String userName);
  6. public String say(String eat);
  7. //public String user(User user);
  8. }

三,编写服务实现类

  1. package com.zcz.cxf.service.impl;
  2. import javax.jws.WebService;
  3. import com.zcz.cxf.service.GreetingService;
  4. @WebService
  5. public class GreetingServiceImpl implements GreetingService {
  6. public String greeting(String userName){
  7. return "你好! " + userName;
  8. }
  9. public String say(String eat) {
  10. return "该吃饭了"+eat;
  11. }
  12. }

四,配置spring启动时,加载的xml文件,按照下面的xml在原有的基础上进行添加编辑,只添加我标注的红色部分即可

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  9. <import resource="classpath:META-INF/cxf/cxf.xml" />
  10. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  11. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  12. <jaxws:endpoint id="greetingService"
  13. implementor="com.gary.test.ws.service.impl.GreetingServiceImpl"
  14. address="/GreetingService" />
  15. </beans>

xmlns:jaxws=http://cxf.apache.org/jaxws

http://cxf.apache.org/jaxwshttp://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" />

<jaxws:endpoint id="greetingService"implementor="com.gary.test.ws.service.impl.GreetingServiceImpl" address="/GreetingService" />

五,配置web.xml对于webservice的调用,在web.xml中添加以下代码即可

  1. <servlet>
  2. <servlet-name>CXFServlet</servlet-name>
  3. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>CXFServlet</servlet-name>
  8. <url-pattern>/cxf/*</url-pattern>
  9. </servlet-mapping>

六,启动项目如果访问http://localhost:8080/springmvcModel/cxf,出现如下图所示的内容则表示基于cxf的webservice配置成功

七,客户端对于该接口的调用

首先,新建一个与服务器端相同的服务接口类GreetingService

其次,写调用服务的类

  1. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  2. public class TestGreetingService {
  3. public static void main(String[] args) {
  4. //创建WebService客户端代理工厂
  5. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  6. //注册WebService接口
  7. factory.setServiceClass(GreetingService.class);
  8. //设置WebService地址
  9. factory.setAddress("http://localhost:8080/springmvcModel/cxf/GreetingService");
  10. GreetingService greetingService = (GreetingService)factory.create();
  11. System.out.println("开始调用webservice...");
  12. System.out.println("返回的信息是:"+greetingService.say("米饭"));
  13. }
  14. }

配置成功,做完之后发现其实很简单,虽然不明白原理,但是会做基本的应用。

使用CXF做webservice整合现有项目的例子的更多相关文章

  1. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  2. 使用 CXF 做 webservice 简单例子

    Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量 ...

  3. jfinal集成spring cxf做webservice服务

    链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...

  4. 使用 CXF 做 webservice 简单例子[转]

    Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量 ...

  5. 使用cxf做webservice接口调用

    一.服务端 建javaweb工程,去官网下载所需的cxf接口发布的jar包,导入到工程.官网地址:http://cxf.apache.org/download.html 1.建立调用接口 packag ...

  6. 使用eclipse整合ssh项目的例子--lljf(1)

    最近向自己单独做一个基于ssh的项目,来预习和巩固自己的Java基础.找了一个实际生活中的定做衣服的例子来做一做,放到博客上给大家一起分享学习,后边会持续更新项目编写时候遇到的困难和使用的技术等. 1 ...

  7. CXF之webservice

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  8. CXF WebService整合SpringMVC的maven项目

    首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/de ...

  9. Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端

    WebService服务端项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

随机推荐

  1. C#有关的vshost、exe、config格式说明

    vshost.exe.config是程序运行时的配置文本 exe.config是程序运行后会复制到vshost.exe.config app.config是在vshost.exe.config和exe ...

  2. Ubuntu13.04下Eclipse中文乱码解决

    参考:http://www.linuxidc.com/Linux/2011-12/50056.htm baoyu@baoyu:~$ gedit /var/lib/locales/supported.d ...

  3. otunnel : 一个和lcx差不多的端口转发的工具

    项目地址 ooclab/otunnel 下载地址(内涵各大平台) http://dl.ooclab.com/otunnel/ otunnel 用法 前提: 1. 假设 server 的地址为 exam ...

  4. CPU性能判断指标---上下文切换,运行队列和使用率

    http://blog.chinaunix.net/uid-15007890-id-3064254.html uptime11:35:08 up 21:57,  6 users,  load aver ...

  5. python--条件判断和循环--3

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.if语句 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄, ...

  6. ubuntu压缩

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...

  7. WPF 自定义命令 以及 命令的启用与禁用

    自定义命令:     在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...

  8. 使用Unity制作的一个望远镜特效,在狙击手游戏中非经常见

    仅仅须要编写一个脚本文件,然后就能随意设置放大缩小的速度.以及程度.

  9. MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###

    org.apache.ibatis.exceptions.PersistenceException:  ### Error building SqlSession. ### The error may ...

  10. swift - 移除界面上的所有元素

    下面代码可以遍历移除页面视图上的所有元件: //清空所有子视图 func clearViews() { for v in self.view.subviews as [UIView] { v.remo ...