微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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、  编写服务接口实现类

copy
    @WebService(endpointInterface = "com.cxf.org.CxfTester")  
  1. class CxfTesterImpl implements CxfTester {  
  2.     @Override  
  3.              String str = "发送到:" + sendTo + "信息详情:" + Message;  
  4.         return str;  
  5.     }  
  6. 6、  在WEB-INF/建立一个文件夹名为classes。并且新建一个xml文件、名为application

    Context.xml,代码如下:

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

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

copy
    web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  1.     xmlns="http://java.sun.com/xml/ns/javaee"   
  2.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  4.             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  5.             id="WebApp_ID" version="2.5"               
  6.   display-name>Apache_CXF   welcome-file-listwelcome-file>index.html>index.htm>index.jsp>default.html>default.htm>default.jsp     
  7.     
  8.   <!-- 配置启动加载项目 -->  
  9. context-param         param-name>contextConfigLocation         param-value>WEB-INF/classes/applicationContext.xml<!-- 配置监听 -->  
  10. listenerlistener-class         org.springframework.web.context.ContextLoaderListener  
  11.   <!-- 部署servlet -->  
  12. servletservlet-name>CXFServletservlet-class         org.apache.cxf.transport.servlet.CXFServlet  
  13. load-on-startup>1servlet-mappingurl-pattern>/*web-app 8、  发布项目、启动tomcat并且访问wsdl文件

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


    源码下载地址:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐