本文的前提是已经有一个Spring的项目,在此基础上如何跟apache CXF进行结合,开发出WebService服务和调用WebService服务。

1.开发WebService

  1.引入jar包

  下载最新的jar包,并引入:\apache-cxf-3.0.1\lib\*(当然里面有些是不必要的,有兴趣的可以自己删减)。

  2.修改web.xml

<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

  3.引入单独的spring配置文件cxf-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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-servlet.xml" />
<jaxws:endpoint id="UserService"
implementor="cn.telchina.standard.service.user.UserServiceImpl" address="/UserService" />
</beans>
<!-- END SNIPPET: beans -->

上述代码中:

    jaxsw:endpoint是关键。

在web.xml中加入配置

  4.开发服务接口和服务实现类

接口类:

package cn.telchina.standard.service.user;

import javax.jws.WebService;

/**
* 必须要有接口
* @author Administrator
*
*/
@WebService
public interface UserService { public boolean updateTheUser(String user); public boolean updateUsersCode(String user); }

接口实现类:

package cn.telchina.standard.service.user;

import javax.jws.WebService;

@WebService(endpointInterface = "cn.telchina.standard.service.user.UserService")
public class UserServiceImpl implements UserService { @Override
public boolean updateTheUser(String user) {
// TODO Auto-generated method stub
return false;
} @Override
public boolean updateUsersCode(String user) {
// TODO Auto-generated method stub
return false;
} }

 

此时发布程序即可:

http://localhost:8080/cxfProject/services

http://localhost:8080/cxfProject/services/UserService?wsdl

2.调用WebService

调用CXF服务的方式有三种:

1.配置spring bean的方式

   新增配置文件:client-beans,并在web.xml中配置。

Client-beans.xml内容

<?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:oxm="http://www.springframework.org/schema/oxm"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="client"
address="http://localhost:8080/cxfProject/services/UserService"
serviceClass="cn.telchina.standard.service.user.UserService" />
</beans>

java代码:

public static void invokeBySpring(){
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "client-beans.xml" }); UserService client = (UserService) context.getBean("client"); boolean response = client.updateTheUser("Joe");
System.out.println("Response: " + response);
}

2.CXF的方式

public static void invokeService(){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8080/cxfProject/services/UserService");
UserService service = (UserService) factory.create(); boolean response = service.updateTheUser("Joe");
System.out.println("#############"+response+"##############");
}

3.RPC方式

public static void invokeService2() throws Exception{
//这个是用cxf 客户端访问cxf部署的webservice服务
//千万记住,访问cxf的webservice必须加上namespace ,否则通不过
//现在又另外一个问题,传递过去的参数服务端接收不到
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/cxfProject/services/UserService?wsdl");
//url为调用webService的wsdl地址
QName name=new QName("http://user.service.standard.telchina.cn/","updateTheUser");
//namespace是命名空间,methodName是方法名
String xmlStr = "name";
//paramvalue为参数值
Object[] objects=client.invoke(name,xmlStr);
//调用web Service//输出调用结果
System.out.println(objects[0].toString());
}

 

PS:

   cxf的类库和axis2的类库不能放到一个项目中,否者会报错:

Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: REFLECTION

Spring与apache CXF结合实例的更多相关文章

  1. Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

    准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...

  2. Spring 4 集成Apache CXF开发JAX-RS Web Service

    什么是JAX-RS 在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice. 工程概 ...

  3. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  4. Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

    实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...

  5. Spring 3 整合Apache CXF WebService[转]

    http://www.cnblogs.com/hoojo/archive/2012/07/13/2590593.html 在CXF2版本中,整合Spring3发布CXF WebService就更加简单 ...

  6. apache cxf笔记之Spring客户端访问和调用webservice服务

    继续上次的spring服务的问题,这边介绍一种spring配置客户端的方法. 1.src目录下新建client-beans.xml文件 <?xml version="1.0" ...

  7. 使用Apache CXF和Spring集成创建Web Service(zz)

    使用Apache CXF和Spring集成创建Web Service 您的评价:       还行  收藏该经验       1.创建HelloWorld 接口类 查看源码 打印? 1 package ...

  8. Apache CXF实现Web Service(1)——不借助重量级Web容器和Spring实现一个纯的JAX-WS web service

    废话少说,先在Eclipse中新建一个Java Project (可以不是WTP的Dynamic Web Project) 选择Java Project 再看pom.xml 我们使用cxf 3.1.4 ...

  9. spring boot 集成 Apache CXF 调用 .NET 服务端 WebService

    1. pom.xml加入 cxf 的依赖 <dependency> <groupId>org.apache.cxf</groupId> <artifactId ...

随机推荐

  1. Windows2008下搭建NFS实现windows空间提供linux使用

    我们既然是要把Windows 的硬盘给Linux使用  就需要在Windows上安装NFS网络文件系统 操作如下 1.添加角色-----文件服务---勾选网络文件服务 2.安装完成后 我们需要把这30 ...

  2. C# 测试程序运行时间和cpu使用时间

    方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...

  3. thinkphp foreach循环生成二维数组的方法

    先做个问题记录,另外下面是做的过程中遇到的一个没想明白的现象 foreach($result as $key => $val ){ $wzList[$key]['lik']=$val[0]; $ ...

  4. C-链表的一些基本操作【创建-删除-打印-插入】

    #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define LEN sizeof(stru ...

  5. 相比于汇编语言的准确性c语言延时精确度如何提升

    只要合理的运用,C还是可以达到意想不到的效果.很多朋友抱怨C效率比汇编差了很多,其实如果对Keil C的编译原理有一个较深入的理解,是可以通过恰当的语法运用,让生成的C代码达到最优化.即使这看起来不大 ...

  6. 16.python中的浅拷贝和深拷贝

    在讲什么是深浅拷贝之前,我们先来看这样一个现象: a = ['scolia', 123, [], ] b = a[:] b[2].append(666) print a print b

  7. windows下python+flask环境配置详细图文教程

    本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+fla ...

  8. SQL基础篇----select语句与排序问题

    一.检索--输出所有的列 SELECT * FROM my_friends WHEREfirst_name = 'cake'; 知识点1 * 代表选择出所有的行-----(什么行呢?)就是first_ ...

  9. UVA 10970 第一次比赛 D题 (后面才补的)

    Mohammad has recently visited Switzerland. As heloves his friends very much, he decided to buy somec ...

  10. 函数调用和inline作用

    函数调用的开销: 函数被调用时,要有函数调用和返回.要保存当前程序上下文信息,以便函数调用完毕后返回原来的地方,继续执行程序.将函数的参数进行压栈.出栈,执行函数,函数调用完毕后释放内部变量占用的内存 ...