Cxf + Spring3.0 入门开发WebService
转自原文地址:http://sunny.blog.51cto.com/182601/625540/
由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻了很多资料,最终决定使用Cxf + Spring的框架,原因很简单,就是因为Cxf功能强大并且可以和Spring无缝集成。
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。
首先说明一下项目中使用的jar包如下图,服务器端项目项目名称 CxfSpringServer, 客户端项目名称为 CxfSpringClient 两个项目使用的都是以下的jar包, 程序中没有使用最新的cxf-2.4.1版本因为, 该版本不知道是否有问题, 启动的时候总是报错。Spring使用的是3.0。

第一部分开发服务器端:
1: 开发接口程序,接口中定义了serviceName的名称,以及命名空间,注意这个命名空间就是将来客户端中生成的接口的package的路径。
另外@WebResult(name="out") 这句话定义了wadl文件中输出数据的名字,cxf中默认的输出名称是
return,当时要改这个名字也花了不少时间。需要改动这个名称的朋友请注意这个细节。
package com.cxf.bo;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(serviceName="IWsTestBO",targetNamespace="http://impl.ws.com")
public interface IWsTestBO {
@WebResult(name="out")
public String execute(@WebParam(name = "arg0",targetNamespace="http://impl.ws.com")String arg0);
}
2: 接下来开发IWsTestBO 接口的实现类。
@Service("wsServiceBO") 采用的是spring3.0的注解开发。
package com.cxf.bo.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import com.cxf.bo.IWsTestBO;
@Service("wsServiceBO")
@WebService(targetNamespace="http://impl.ws.com")
public class WsTestBOImpl implements IWsTestBO{
public String execute(String arg0) {
return "欢迎 " + arg0 + ",调用WebService服务....";
}
}
3: Spring的配置文件 bo-context.xml。配置文件中定义了WebService的相关属性,注意配置文件中的命名空间的定义是必不可少的。jaxws:endpointbiaoq标签定义了提供Web服务的 Bean 访问地址。 并且配置了服务器接受数据的日志配置,当服务器接受到访问数据时jaxws:features标签配置可以将最原始的日志打印到控制台上。
<?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:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="com.cxf.bo"/> <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="wsServiceBean" implementor="#wsServiceBO" address="/execute" publish="true" >
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>
</jaxws:endpoint>
</beans>
4:接下来是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/bo-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
5: 到现在为止服务器端的程序已经开发完成,是不是很简单。现在可以部署到Tomcat6.0服务器。在部署服务器的时候还有一个需要注意的问题。部署tomcat没有问题,但是部署到resin服务器的时候就会报异常: property "javax.xml.stream.supportDTD" not supported 。上网查询了一些资料,发现的确是resin服务器有问题, 改进方案是, 首先在resin.conf配置中找到如下代码:
<!-- Uncomment to use Resin's XML implementations
- <system-property javax.xml.parsers.DocumentBuilderFactory
- ="com.caucho.xml.parsers.XmlDocumentBuilderFactory"/>
- <system-property javax.xml.parsers.SAXParserFactory
- ="com.caucho.xml.parsers.XmlSAXParserFactory"/>
-->
如果已经使用,就替换下面,如果没有使用,就加上下面的配置
<system-property javax.xml.stream.XMLInputFactory="com.sun.xml.internal.stream.XMLInputFactoryImpl" />
部署之后访问如下地址 http://localhost:9113/CxfSpringServer/services/execute?wsdl 我电脑用的是9113的端口,你的肯定和我的是不一样的,所以你访问的时候改一下端口就可以了。地址访问成功时候会出现一个xml的配置文件的信息,这 里就不展示了。
第二部分开发客户端:
1: 客户端和服务器端是两个独立的应用,jar包用的是同一组。如果你用myeclipse的话可以通过myeclipse自带的webservice客户端 生成向导生成客户端接口。根据地址生成客户端这样比较方便,但是生成的文件中除了接口类之外,其它都没什么用的,可以删掉。

2: 客户端接口类如下
package com.ws.impl;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
@WebService(name = "IWsTestBO", targetNamespace = "http://impl.ws.com")
public interface IWsTestBO {
@WebMethod
@WebResult(name = "out", targetNamespace = "")
@RequestWrapper(localName = "execute", targetNamespace = "http://impl.ws.com", className = "com.ws.impl.Execute")
@ResponseWrapper(localName = "executeResponse", targetNamespace = "http://impl.ws.com", className = "com.ws.impl.ExecuteResponse")
public String execute(@WebParam(name = "arg0", targetNamespace = "http://impl.ws.com") String arg0);
}
3: 客户端的Spring配置文件如下:
<?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:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.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="wsClient" class="com.ws.impl.IWsTestBO" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.ws.impl.IWsTestBO"/>
<property name="address" value="http://localhost:9113/CxfSpringServer/services/execute?wsdl"/>
</bean>
</beans>
4: 客户端主函数测试类如下:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ws.impl.IWsTestBO; public class WsTestClient {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"bo-context.xml"});
IWsTestBO client = (IWsTestBO)ctx.getBean("wsClient");
String result = client.execute("张健");
System.out.println(result);
}
}
5:服务器端输入输出日志如下:
信息: Inbound Message
----------------------------
ID:
Address: /CxfSpringServer/services/execute
Encoding: UTF-
Content-Type: text/xml; charset=UTF-
Headers: {cache-control=[no-cache], content-type=[text/xml; charset=UTF-], connection=[keep-alive], host=[localhost:], Content-Length=[], SOAPAction=[""], user-agent=[Apache CXF 2.2.], Content-Type=[text/xml; charset=UTF-], Accept=[*/*], pragma=[no-cache]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:execute xmlns:ns1="http://impl.ws.com"><arg0 xmlns="http://impl.ws.com">张健</arg0></ns1:execute></soap:Body></soap:Envelope>
--------------------------------------
2011-7-29 12:59:07 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
信息: Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:executeResponse xmlns:ns1="http://impl.ws.com"><out>欢迎 张健,调用WebService服务....</out></ns1:executeResponse></soap:Body></soap:Envelope>
--------------------------------------
关于Cxf + Spring3.0 集成开发WebService服务的入门例子程序就写到这里,有问题的童鞋可以留言。我们一起讨论, 谢谢!
Cxf + Spring3.0 入门开发WebService的更多相关文章
- Spring3.0 入门进阶(1):从配置文件装载Bean
Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅. 作为入门,本篇主要介绍Bean的加载 ...
- Spring3.0 入门进阶(三):基于XML方式的AOP使用
AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...
- CXF 开发 WebService
什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...
- axis1.4开发webservice服务端(快速入门)-基于jdk1.4
写在前面: 现在有很多开发webservice的方法以及框架,什么cxf等,但是这些在你编写服务类的时候都要用到注解这个功能.如果现在的jdk是1.4的,那么就不能使用注解这个功能了.所以这里可以用到 ...
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...
- 基于CXF框架下的SOAP Webservice服务端接口开发
最近对webservice 进行入门学习,网上也是找了很多的学习资料.总得感觉就是这了解点,那了解点.感觉不够系统,不够容易入门.差不多断断续续看了一个星期了,今天小有成果,把客户端,服务端都搞定了. ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
以下文章来自 http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...
- Spring Boot用Cxf的jax-ws开发WebService
首先上项目的pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...
随机推荐
- 布隆过滤器(Bloom Filter)
一.布隆过滤器介绍 Bloom Filter是一种空间效率很高的随机数据结构,Bloom Filter可以看做是对bit-map的扩展,它的原理如下: 当一个元素被加入集合时,通过K个Hash函数将这 ...
- TCL 双引号和花括号的区别
用了很一段时间的Modelsim,用TCL脚本仿真,开始注意起来它,看了些资料,有一个问题始终让我很困惑,那就是花括号 的用法,今天Google了一下,觉得有一点豁然了,记录一下.为了不让自己的翻译影 ...
- 无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本。
引自:http://www.cnblogs.com/chillsrc/archive/2010/06/28/1766816.html 安装完VS2010中文版之后,又安装了Silverlight4_T ...
- 状态栏通知Notification的简单使用
今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下. 在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示.而在Android中有提醒功能 ...
- JAVA长连接demo
http://blog.csdn.net/caomiao2006/article/details/38830475 JAVA长连接demo 2014-08-25 23:20 4767人阅读 评论(2) ...
- Xcode中需要熟悉的常用快捷键
因为工作需要,笔者最近开始接触Xcode这款Mac系统下的强大的编程软件.因为个人习惯,每当接触新的软件的时候总会先去了解它的一些常用快捷键.经过多方查阅总结出以下内容,希望对刚刚接触Xcode的初学 ...
- Hdu 4514 湫湫系列故事——设计风景线
湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- caffe源码阅读(3)-Datalayer
DataLayer是把数据从文件导入到网络的层,从网络定义prototxt文件可以看一下数据层定义 layer { name: "data" type: "Data&qu ...
- IOS 学习笔记 2015-03-24 OC-API-不可变字符串
大部分是模仿// // main.m // OC-API-不可变字符串 // // Created by wangtouwang on 15/3/25. // Copyright (c) 2015年 ...
- Java中printStackTrace()、toString()、getMessage()的区别
一.三者之间的关系图: 二.演示 1.printStackTrace()演示: public class Test { public int div(int a, int b) { ...