由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的。以下是步骤:
第一步:建立一个web项目。
第二步:准备所有jar包。将cxf_home\lib项目下的所有jar包全部copy到新项目的lib目录下,里面已经包含了spring3.0的jar包。
第三步:在web.xml中配置cxf的核心servlet,CXFServlet。
第四步:创建(最好是Copy)cxf-servlet.xml文件。这是一个spring的配置文件。

1、web.xml中配置servlet

如果没有提供给cxf默认的/WEB-INF/cxf-servlet.xml配置文件,则必须要在spring的配置文件中配置以下三行配置(import)。否则将不能加载名称为cxf的bean.从而启动失败。

2、applicationContext.xml

<!--spring发布webservice服务配置 -->
<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" /> <!-- 注入webservice服务 -->
<!-- 统一工号管理接口 -->
<bean id="UnifiedNoServiceBean" class="com.webservice.unifiedno.service.impl.UnifiedNoServiceImpl" />
<jaxws:server id="UnifiedNoService" serviceClass="com.webservice.unifiedno.service.UnifiedNoService" address="/unifiedNoService">
<jaxws:serviceBean>
<ref bean="UnifiedNoServiceBean" />
</jaxws:serviceBean>
</jaxws:server>

注意:

1、<import>的三个文件是否需要全部引入根据cxf框架版本不同而不同
2、address的值为webservice注解的值:@WebService(serviceName = "unifiedNoService")
3、必须要在声明为serviceClass的接口上声明@WebSerive注解,因为,要使用了接口,在接口上添加的注解才会有效。
4、serviceClass:必须为一个接口,并在接口上必须使用@WebService注解否则调用时会抛出异常
5、serviceBean:是实际服务的类,必须是serviceClass的子类,此类上面即可以使用@WebService注解,也可以不使用
6、address:访问地址,省去前面的ip:port,注意在此注册的类,必须要添加@WebService的注解

3、写接口及实现类

启动项目,测试cxf是否配置成功:
访问:http://localhost:8080/ins/services,会列出所有已经发布的webservice接口服务

4、测试
http://localhost:8080/ins/services/unifiedNoService?wsdl

Java项目代码调用服务:

使用纯Java项目调用
1、根据客户端生成的代码来调用。(优选这种方式)
请先生成然后在任意的Java项目中调用 。
2、客户端只拥有一个接口,使用JaxWsProxyFactoryBean来调用。
因为以下使用了JaxWsProxyFactoryBean,所以,仍然需要CXF的环境,而使用此环境就会造成Jar文件的大量冗余,所以大家要谨慎选择。

1、注意,此处所说的是在Java项目中调用Spring的服务,并不是在JavaEE项目中调用。后期将会讲到如何在JavaEE项目中调用。
2、建议从wsdl地址获取接口文件,也仅需要接口文件。

JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();
client.setAddress("http://localhost:7777/xcxf2_web/ws/one");
client.setServiceClass(IOneService.class);
IOneService one = client.create(IOneService.class);
String ss = one.sayHi("OK你好");
System.err.println(ss);

在Spring项目中,通过配置文件调用:

以下是使用Spring的配置文件调用:
新建立一个Java项目,并加载cxf的所有包。
只需要生成的接口文件。
在classpath下新建立一个ClientBeans.xml文件.

优点与缺点:
此种情况,适合于一个Javaweb项目已经集成了Spring。并希望通过CXF配置的方式调用Web服务。
此种情况,仍然需要导入CXF的大量jar包。
这种情况也存在一定人优点,如可以将外部的Web服务通过配置文件注入(DI)到Action类中。

说明:
通过<jaxws:client/>来获取WebService,id就不用说了吧。
address是不包含?wsdl的服务地址。
serviceClass是本地的接口名,与服务接口名保持相同才可以。

1、以下是ClientBeans.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<jaxws:client id="helloClient"
address="http://127.0.0.1:9999/cxf2.4_spring_web/ws/helloworld"
serviceClass="com.itcast.cxf.first.IHelloWorld">
</jaxws:client>
</beans>

1、以下是CxfJavaClient.java的源代码:

package com.itcast.cxfweb.java.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itcast.cxf.first.IHelloWorld;
/**
* Java项目的客户端
* @author wangjianme
*/
public class CxfJavaClient {
public static void main(String[] args) {
//读取配置文件
ApplicationContext ctx =
new ClassPathXmlApplicationContext("ClientBeans.xml");
//get到接口类型并调用
IHelloWorld hello = (IHelloWorld)ctx.getBean("helloClient");
String str = hello.sayHello("WJ");
System.err.println(str);
}
}

在本域使用jquery访问: --查询所有用户:

<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
var url = "http://localhost:7777/ws2/ws/user";
var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
'xmlns:q0="http://service.ws2.itcast.cn/" '+
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Body><q0:getUsers/></soapenv:Body></soapenv:Envelope>';
$.ajax({
url:url,//访问的url
dataType:'xml',//返回的数据类型
type:'post',//请求方式
contentType:'application/soap+xml;charset=UTF-8',
data:soap,//数据
success:function(data,status,xhr){
//对返回后的数据进行解析
$(data).find("return").each(function(){
var nm = $(this).find("name").text();
var age = $(this).find("age").text();
alert(nm+","+age);
});
},
error:function(xhr,status){
alert("出错了:"+status);
}
});
});
});
</script>

向服务器保存用户:

以下是jsclient.jsp的源代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<script type="text/javascript"
src="<c:url value='/js/jquery-1.5.js'/>"></script>
</head>
<body>
<label for="name">姓名:</label>
<input type="text" id="name" name="name"/>
<br/>
<a href="#" id="ok">确定</a>
</body>
<script type="text/javascript">
$(function(){
$("#ok").click(function(){
var val = $("#name").val();
if($.trim(val)==""){
alert("请输入名称");
return;
}
var str = '<?xml version="1.0" encoding="UTF-8"?>'+
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<soap:Body><ns2:sayHello xmlns:ns2="http://first.cxf.itcast.com/">'+
'<arg0>'+val+'</arg0>'+
'</ns2:sayHello></soap:Body></soap:Envelope>';
$.ajax({
contentType:'application/xml;charset="UTF-8"',
dataType:'xml',
type:'post',
url:'http://localhost:9999/cxf2.4_spring_web/ws/helloworld', //直接发向这个地址
data:str,
success:function(data){
//$(data).find("return").each(function(){
// alert($(this).text());
//}); //使用上面的方法也是可以的
var ss = $(data).find("return").first().text();
$("<div>").html(ss)
.css("border","1px solid blue")
.css({width:'50%'}).
appendTo($("body"));
$("#name").val("");
}
},"xml");
});
});
</script>
</html>

spring集成cxf实现webservice接口功能的更多相关文章

  1. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  2. Spring MVC + CXF实现webservice接口

    本来都是WebAPI/RestfulAPI接口对外提供接口的,突然有个需求要提供WebService接口,本想着Spring和CXF这么成熟的两个产品,这么的也整不出什么幺蛾子来啊. 结果还真是出了几 ...

  3. 使用CXF与Spring集成实现RESTFul WebService

    以下引用与网络中!!!     一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存 ...

  4. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

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

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

  6. 使用cxf开发webservice接口

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

  7. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  8. 脱离spring集成cxf(基于nutz框架)

    什么是webService WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理论资料: http://blog.csdn.net/wooshn/article/details/8 ...

  9. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

随机推荐

  1. 学习总结:CSS(一)定义方式、选择器、选择器权重

    一.CSS的定义方式 1.内部样式:<style></style> 2.行间样式:<div style="width:100px;height:100px;&q ...

  2. vue常用指令

    1.v-if系列 v-if="数据|判断" 只要条件成立,就显示if中的元素 v-else (注意:必须跟在v-if或者v-if-else的后面,不然失效) 如果if条件不成立显示 ...

  3. java四种权限修饰符(public > protected > (default) > private)

    权限修饰符在哪里可以访问 (default) : 表示什么权限修饰符都不写 位置 public protected (default) private 同一个类 yes yes yes yes 同一个 ...

  4. JAVA正确地自定义比较对象---如何重写equals方法和hashCode方法

    在实际应用中经常会比较两个对象是否相等,比如下面的Address类,它有两个属性:String province 和 String city. public class Address { priva ...

  5. idea 创建运行web项目时,报错: Can not issue executeUpdate() for SELECTs解决方案

    最近在做一个Web课程设计的时候遇到了如下的问题. java.sql.SQLException: java.lang.RuntimeException: java.sql.SQLException: ...

  6. [译]kendoui - 方法和事件

    原文 为了使用方法和事件,首先要获取到widget实例. 获取widget 一共有3种获取widget的方式. jQuery的data方法 将widget的名作为参数传给jQuery的data方法.( ...

  7. luogu 2296 寻找道路 简单BFS

    简单的BFS,练习基础 #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i<=y;i++) #defi ...

  8. Web获取客户端物理MAC地址(ocx插件)ActiveX控件

    主要是通过ActiveX控件 从本地获取到MAC地址,传入到浏览器打开的网页中,再提交到服务器. 具体详解与步骤看文档中: 文件实例包下载 DotNetFX 文件夹附件文件:(可能安装时需用) dot ...

  9. KMP模板(HDU1711)

    #include<stdio.h> #include<math.h> #include<string.h> #include<stack> #inclu ...

  10. LOJ #2026「JLOI / SHOI2016」成绩比较

    很好的锻炼推柿子能力的题目 LOJ #2026 题意 有$n$个人$ m$门学科,第$ i$门的分数为不大于$U_i$的一个正整数 定义A「打爆」B当且仅当A的每门学科的分数都不低于B的该门学科的分数 ...