Apache CXF+Spring开发环境搭建小试

最近手上一个项目要开发webservice,而原有系统使用了spring,所以在选择框架的时候,我选择了cxf,这样在开发整合的时候就比较方便了。在搭建开发环境的过程中发现这篇文章写得比较详细,所以就搬到自己博客里,希望给自己和同行做参考。

CXF 应用开发
下面就将开始我们的 CXF Web Services 的开发之旅!首先,要有一个基于 Eclipse 的开发环境;然后,我们将利用这个开发环境开发一个简单的“调查投票”示例,同时我们将解释一些 CXF 在开发中进行配置的基本方法。

开发环境准备
在开发之前我们通常需要做些准备工作,比如搭建好开发环境。在本教程中,我们将在 Eclipse 3.2 环境中开发,当然如果您是使用 Eclipse Europa(Eclipse 3.3)也是可以的,我们假设您已经下载并且在计算机中安装好了 Eclipse 开发环境(关于如何下载和安装 Eclipse,请参见 参考资源)。


创建项目骨架
启动 Eclipse,创建一个 Java Project,如果是 WTP 的话,可以直接创建一个 J2EE 的 Web 项目,我们取名为 CXF_Spring_Survey,并设置编译的 output 路径为 WEB-INF/classes 目录,方便直接部署应用程序。
目录结构如下图所示:

图 4. 利用 CXF 开发 Web Services 的工程骨架示意图


为了方便起见,我们直接拷贝 %CXF_HOME%/lib 目录下的所有 .jar 文件到 CXF_Spring_Survey 项目的 WEB-INF/lib 目录下,也可以根据前面“CXF 安装包”章节所述的各个 jar 包的作用范围选择仅需要的 .jar 文件。在 Eclipse 里刷新后,可以看到如下结构:

图 5. 利用 CXF 开发 Web Services 的工程引入所有 .jar 文件后的骨架示意图


并在 CXF_Spring_Survey 项目属性里将这些 .jar 加到 Java Build Path 当中去,如下图:

图 6. Eclipse 中引入所有 .jar 文件后的示意图


这样,项目的基本骨架已经创建完成,接下来开始编写接口与具体实现的代码了。

接口类创建
在项目的 src 目录中新建一个 ws.cxf 包,并在里面创建接口类 ISurveyService.java,为了简单示示例起见,我们仅创建一个方法 public String vote(String username,int point); 这里要注意的是我们在接口上用 @WebService 注解标明这是一个即将暴露为 Web Service 的接口,并将里面的方法都暴露出去。完整的接口代码清单如下:

Java代码 收藏代码
  1. packagews.cxf;
  2. importjavax.jws.WebService;
  3. @WebService
  4. publicinterfaceISurveyService
  5. {
  6. /**
  7. *@paramusername名字
  8. *@parampoint分数
  9. *@return
  10. */
  11. publicStringvote(Stringusername,intpoint);
  12. }


接下来,我们根据接口的定义,来实现它。

具体类实现
针对接口的定义,我们创建一个相应的实现类,并将其定义在 sw.cxf.impl 包中,完整的代码清单如下:

Java代码 收藏代码
  1. packagews.cxf.impl;
  2. importjavax.jws.WebService;
  3. importws.cxf.ISurveyService;
  4. @WebService
  5. publicclassSurveyServiceimplementsISurveyService
  6. {
  7. privateStringexcludeName="Michael";
  8. privateintleastPonit=5;
  9. publicStringvote(Stringusername,intpoint)
  10. {
  11. Stringresult="";
  12. if(excludeName.equals(username))
  13. {
  14. result="您不能重复进行投票!";
  15. }
  16. else
  17. {
  18. result="谢谢您的投票!";
  19. if(point<leastPonit)
  20. {
  21. result+="您的投票分数太低!";
  22. }
  23. else
  24. {
  25. result+="您的投票分数通过审核!";
  26. }
  27. }
  28. returnresult;
  29. }
  30. //ForIoC
  31. publicStringgetExcludeName()
  32. {
  33. returnexcludeName;
  34. }
  35. publicvoidsetExcludeName(StringexcludeName)
  36. {
  37. this.excludeName=excludeName;
  38. }
  39. publicintgetLeastPonit()
  40. {
  41. returnleastPonit;
  42. }
  43. publicvoidsetLeastPonit(intleastPonit)
  44. {
  45. this.leastPonit=leastPonit;
  46. }
  47. }



接口定义与具体的实现就这样简单完成了,接下来就是相关的配置工作了,首先进行 Spring 的 Bean 配置。


Spring 配置
在 src 目录中创建 beanRefServer.xml 文件,用来定义 Spring 的 Bean 的配置,CXF 支持 Spring 2.0 Schema 标签配置方式,并且提供快捷暴露 Web Services 的标签。

首先,我们需要引入 Spring 与 CXF 的命名空间(namespace),如下:

Java代码 收藏代码
  1. <beansxmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://cxf.apache.org/jaxws
  8. http://cxf.apache.org/schemas/jaxws.xsd">



这样,我们可以使用 Spring 与 CXF 的标签配置了。接着,我们需要引入我们所需要的 CXF 的 Bean 定义文件,如下:

Java代码 收藏代码
  1. <!--ImportApacheCXFBeanDefinition-->
  2. <importresource="classpath:META-INF/cxf/cxf.xml"/>
  3. <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  4. <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>


接着定义我们具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的:

Java代码 收藏代码
  1. <!--SurveyService-->
  2. <beanid="surveyService"class="ws.cxf.impl.SurveyService">
  3. <propertyname="excludeName"value="Michael"/>
  4. <propertyname="leastPonit"value="10"/>
  5. </bean>


最后,将定义的 Bean 暴露出去成为 Web Service 服务,通过 CXF 提供的 Schema 标签配置 <jaxws:server> ,这样定义的配置显得更加简洁与方便,定义如下:

Java代码 收藏代码
  1. <!--ExposeSurveyWebService-->
  2. <jaxws:serverid="surveyWebService"
  3. serviceClass="ws.cxf.ISurveyService"
  4. address="/SurveyWebService">
  5. <jaxws:serviceBean>
  6. <refbean="surveyService"/><!--要暴露的bean的引用-->
  7. </jaxws:serviceBean>
  8. </jaxws:server>



在配置中,serviceClass 的值是我们的接口类的名称,address 为将要暴露出去的 Web Service 访问地址。比如:/SurveyWebService,那么客户端消费 Web Service 的地址就会成为 http://host:port/WebAPPName/SurveyWebService ,与之相应的 WSDL 地址则为: http://host:port/WebAPPName/SurveyWebService?wsdl 。

Web 应用配置
由于我们的示例是需要通过 Servlet 容器进行服务暴露,因此需要配置相对应的 web.xml 文件,首先是增加 Spring 的配置文件加载 Listener,如下:

Java代码 收藏代码
  1. <!--SpringConfigLocation-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
  5. </context-param>
  6. <!--SpringContextLoaderListener-->
  7. <listener>
  8. <listener-class>
  9. org.springframework.web.context.ContextLoaderListener
  10. </listener-class>
  11. </listener>


接下来配置 CXF Servlet 的定义,以及它的映射,如下:

Java代码 收藏代码
  1. <!--ApacheCXFServlet-->
  2. <servlet>
  3. <servlet-name>CXFServlet</servlet-name>
  4. <display-name>CXFServlet</display-name>
  5. <servlet-class>
  6. org.apache.cxf.transport.servlet.CXFServlet
  7. </servlet-class>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
  10. <!--CXFServletMapping-->
  11. <servlet-mapping>
  12. <servlet-name>CXFServlet</servlet-name>
  13. <url-pattern>/*</url-pattern>
  14. </servlet-mapping>



我们将之映射为 /* 。这样,服务端的代码与配置就全部完成了,接下来就是将应用程序部署到 Web 容器中去,并验证服务是否正常发布。

应用部署


我们将应用部署到 Tomcat 5.5.25 当中,在这里,我们采用链接(Link)的部署方式,简单而方便,在 %TOMCAT_HOME%/conf/Catalina/localhost/ 目录下创建与项目名称 CXF_Spring_Survey 一致的 xml 文件:CXF_Spring_Survey.xml,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="F:/JavaProject/WebService/CXF/CXF_Spring_Survey"/>

docBase 里的内容根据您的实际项目所在的目录进行更改,注意使用 / 而不是 即可。

启动服务
这时开始启动 Tomcat ,在启动的过程中,可以在启动窗口上看到以链接方式部署的应用在启动中会打印出一些相关信息来,最后显示启动成功。 通过访问 http://localhost:8080/CXF_Spring_Survey/ 可以看到 CXF 暴露的服务链接:

图 7. CXF 暴露的服务链接的内容示意图


可以直接点击进去,或者手工输入 WSDL 的地址进行访问:http://localhost:8080/CXF_Spring_Survey/SurveyWebService?wsdl ,可以看到如下的 WSDL 内容:

图 8. SurveyWebService 的 WSDL 内容示意图


这样,我们可以确定我们的服务真正发布成功了,接下来就可以利用客户端进行消费了。

消费服务
回到 Eclipse 开发平台,开始编写消费服务相关的代码,首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src 目录下创建 beanRefClient.xml 配置文件,同样,我们也需要引入 Spring 与 CXF 命名空间的声明,并引入 CXF 的 Bean 的定义文件,最后通过与服务端配置相对的 CXF 标签 <jaxws:client> 来定义客户端访问服务的声明,完整的定义内容如下:

Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://cxf.apache.org/jaxws
  9. http://cxf.apache.org/schemas/jaxws.xsd">
  10. <!--ImportApacheCXFBeanDefinition-->
  11. <importresource="classpath:META-INF/cxf/cxf.xml"/>
  12. <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  13. <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  14. <!--SurveyWebServiceClient-->
  15. <jaxws:clientid="surveyServiceClient"
  16. serviceClass="ws.cxf.ISurveyService"
  17. address="http://localhost:8080/CXF_Spring_Survey/SurveyWebService"/>
  18. </beans>



定义说明:id 为 Spring 定义的 id,用来在程序里进行获取它的标识,serviceClass 仍是为服务端定义的接口类,address 为完整的 Web Service 地址,这个与服务端的定义不一样。
定义完配置文件,接下来我们编写访问的具体代码,在 test 目录下创建 ws.cxf.client 包,然后创建 SurveyServiceClient.java,完整的代码如下:

Java代码 收藏代码
  1. packagews.cxf.client;
  2. importorg.springframework.context.ApplicationContext;
  3. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. importws.cxf.ISurveyService;
  5. publicclassSurveyServiceClient
  6. {
  7. publicstaticvoidmain(String[]args)
  8. {
  9. //加载客户端的配置定义
  10. ApplicationContextcontext=new
  11. ClassPathXmlApplicationContext("beanRefClient.xml");
  12. //获取定义的WebServiceBean
  13. ISurveyServicesurveyService=
  14. (ISurveyService)context.getBean("surveyServiceClient");
  15. //1、定义调查投票的变量与内容,用来发送给服务
  16. Stringusername="Test";
  17. intpoint=88;
  18. //调用方法进行服务消费
  19. Stringresult=surveyService.vote(username,point);
  20. System.out.println("Result:"+result);
  21. //2、传递不一样的调查投票内容
  22. username="Michael";
  23. point=100;
  24. //再次调用方法进行服务消费,得到不一样的结果
  25. result=surveyService.vote(username,point);
  26. System.out.println("Result:"+result);
  27. //3、第三次传递与调用
  28. username="Jordan";
  29. point=9;
  30. result=surveyService.vote(username,point);
  31. System.out.println("Result:"+result);
  32. }
  33. }



直接运行以上客户端消费程序,一共调用了三次 Web Service,并得到结果如下:
Result: 谢谢您的投票!您的投票分数通过审核!
Result: 您不能重复进行投票!
Result: 谢谢您的投票!您的投票分数太低!

于是服务正常地得到了调用,并且能够正确地返回结果,完整的代码及配置文件可以在本教程的 下载链接 里进行下载。


原文地址:http://www.ibm.com/developerworks/cn/education/java/j-cxf/section5.html

原文地址:https://www.cnblogs.com/zhuifengji/p/4270920.html