从网上看了很多CXF的资料,大部分都是单独的作为一个webservice项目,对于在现有的spring项目上提供webservice服务的例子基本没有找到。
我做的这个例子是介绍怎么把cxf整合到现有的spring项目中,现在只做到可以传简单的字符串和JAVABEAN,复杂的以后研究。
一,应用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一系列的架包
二,首先是服务接口
package com.zcz.cxf.service; import javax.jws.WebService; @WebService public interface GreetingService { public String greeting(String userName); public String say(String eat); //public String user(User user); }
三,编写服务实现类
package com.zcz.cxf.service.impl; import javax.jws.WebService; import com.zcz.cxf.service.GreetingService; @WebService public class GreetingServiceImpl implements GreetingService { public String greeting(String userName){ return "你好! " + userName; } public String say(String eat) { return "该吃饭了"+eat; } }
四,配置spring启动时,加载的xml文件,按照下面的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" 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-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" /> </beans>
xmlns:jaxws=http://cxf.apache.org/jaxws
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" />
<jaxws:endpoint id="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
其次,写调用服务的类
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class TestGreetingService { public static void main(String[] args) { //创建WebService客户端代理工厂 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //注册WebService接口 factory.setServiceClass(GreetingService.class); //设置WebService地址 factory.setAddress("http://localhost:8080/springmvcModel/cxf/GreetingService"); GreetingService greetingService = (GreetingService)factory.create(); System.out.println("开始调用webservice..."); System.out.println("返回的信息是:"+greetingService.say("米饭")); } }
配置成功,做完之后发现其实很简单,虽然不明白原理,但是会做基本的应用。