WebService系列博客{十}[CXF简单案例实现]

概述:

CXF是Apache的一个Web Service框架。搭配Jax-Ws使用将部署好的web service发布到tomcat容器中


简单案例:

1、 首先准备好Jar包。可以到apache的官网下载

2、 将下载好的Jar包一次性导入到项目目录[WEB-INF/lib]文件夹下面

3、 解压lib里面的cxf.jar文件,将解压目录下面的[META-INF/cxf]copy到项目的META-INF下面

4、 编写web service接口

[java]  view plain copy
  1. @WebService(name = "CxfTester", targetNamespace = "http://org.cxf.com/")  
  2. public interface CxfTester {  
  3.     public String sendMessage(String sendTo,String message);  
  4. }  

5、  编写服务接口实现类

[java]  view plain copy
  1. @WebService(endpointInterface = "com.cxf.org.CxfTester")  
  2. public class CxfTesterImpl implements CxfTester {  
  3.     @Override  
  4.     public String sendMessage(String sendTo, String Message) {  
  5.         String str = "发送到:" + sendTo + "信息详情:" + Message;  
  6.         return str;  
  7.     }  
  8. }  

6、  在WEB-INF/建立一个文件夹名为classes。并且新建一个xml文件、名为application

Context.xml,代码如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="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.xsd  
  8.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  9.          
  10. <!-- 导入cxf.jar解压出来的文件 -->          
  11. <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  12. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  13. <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  14.   
  15. <!-- 声明Endpoint,   
  16. address 为访问地址的部分地址  
  17. implementor   为实现类  
  18.  -->  
  19. <jaxws:endpoint id="CxFTester"   
  20.     address="/CxfTester"   
  21.     implementor="com.cxf.org.CxfTesterImpl"/>  
  22.   
  23.   
  24.   
  25.       
  26. <!-- 指定客户端访问相关情况  
  27. class   服务端接口  
  28. factory-bean    引用下面声明的类  
  29. factory-method   客户端生成调用对象所用方法  
  30. -->  
  31. <bean id="client"   
  32.         class="com.cxf.org.CxfTester"  
  33.             factory-bean="clientFactory"  
  34.                 factory-method="create"></bean>  
  35.                   
  36.   
  37.               
  38. <!-- 配置CXF服务代理bean  
  39. serviceClass : 服务端接口  
  40. address : 访问地址jaxws:endpoint声明的address在项目名的后面,此处的路径和web.xml配置的urlpattern有联系  
  41.  -->               
  42. <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  43.     <property name="serviceClass" value="com.cxf.org.CxfTester"></property>  
  44.     <property name="address" value="http://192.168.1.104:8080/Apache_CXF_1/CxfTester"></property>  
  45. </bean>  

7、  编辑web.xml,设置容器启动加载applicationContext.xml文件。并且配置CXFServlet

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  7.             id="WebApp_ID" version="2.5">  
  8.               
  9.   <display-name>Apache_CXF</display-name>  
  10.   <welcome-file-list>  
  11.     <welcome-file>index.html</welcome-file>  
  12.     <welcome-file>index.htm</welcome-file>  
  13.     <welcome-file>index.jsp</welcome-file>  
  14.     <welcome-file>default.html</welcome-file>  
  15.     <welcome-file>default.htm</welcome-file>  
  16.     <welcome-file>default.jsp</welcome-file>  
  17.   </welcome-file-list>  
  18.     
  19.     
  20.   <!-- 配置启动加载项目 -->  
  21.   <context-param>  
  22.         <param-name>contextConfigLocation</param-name>  
  23.         <param-value>WEB-INF/classes/applicationContext.xml</param-value>  
  24.   </context-param>  
  25.     
  26.   <!-- 配置监听 -->  
  27.   <listener>  
  28.     <listener-class>  
  29.         org.springframework.web.context.ContextLoaderListener  
  30.     </listener-class>  
  31.   </listener>  
  32.     
  33.   <!-- 部署servlet -->  
  34.   <servlet>  
  35.     <servlet-name>CXFServlet</servlet-name>  
  36.     <servlet-class>  
  37.         org.apache.cxf.transport.servlet.CXFServlet  
  38.     </servlet-class>  
  39.     <load-on-startup>1</load-on-startup>  
  40.   </servlet>  
  41.     
  42.   <servlet-mapping>  
  43.     <servlet-name>CXFServlet</servlet-name>  
  44.     <url-pattern>/*</url-pattern>  
  45.   </servlet-mapping>  
  46. </web-app>  

8、  发布项目、启动tomcat并且访问wsdl文件

http://localhost:8080/Apache_CXF_1/CxfTester?wsdl


源码下载地址:

http://pan.baidu.com/share/link?shareid=216475&uk=1997312776

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值