您的位置:首页 > 其它

使用CXF做webservice整合现有项目的例子

2013-09-08 00:34 337 查看
从网上看了很多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一系列的架包

二,首先是服务接口

packagecom.zcz.cxf.service;

importjavax.jws.WebService;

@WebService
publicinterfaceGreetingService{
publicStringgreeting(StringuserName);
publicStringsay(Stringeat);
//publicStringuser(Useruser);
}

三,编写服务实现类

packagecom.zcz.cxf.service.impl;

importjavax.jws.WebService;

importcom.zcz.cxf.service.GreetingService;

@WebService
publicclassGreetingServiceImplimplementsGreetingService{

publicStringgreeting(StringuserName){
return"你好!"+userName;
}

publicStringsay(Stringeat){
return"该吃饭了"+eat;
}

}


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

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">

<importresource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

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

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

http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd

<importresource="classpath:META-INF/cxf/cxf.xml"/>

<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

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

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

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


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



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

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

其次,写调用服务的类

importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;

publicclassTestGreetingService{

publicstaticvoidmain(String[]args){
//创建WebService客户端代理工厂
JaxWsProxyFactoryBeanfactory=newJaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(GreetingService.class);
//设置WebService地址
factory.setAddress("http://localhost:8080/springmvcModel/cxf/GreetingService");
GreetingServicegreetingService=(GreetingService)factory.create();
System.out.println("开始调用webservice...");
System.out.println("返回的信息是:"+greetingService.say("米饭"));

}

}

配置成功

,做完之后发现其实很简单,虽然不明白原理,但是会做基本的应用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: