(七) CXF 整合Spring--发布WS服务

来源:互联网 发布:数控车床编程g代码 编辑:程序博客网 时间:2024/05/17 11:08

    CXF 可以轻松整合Spring 来发布WS服务。


【1. 引入相关jar 包】

          CXF 使用在WEB项目中,所以就不需要引入 jetty 的相关jar包了,只需要引入以下几个包即可:
         


        引入spring 的相关jar 包,最终引入包结构:

   

【2. 配置web.xml】

      1. 配置spring 配置文件路径

      2. 配置spring 监听

      3. 配置log4j 日志框架

      4. 配置CXF 拦截器

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><!-- 配置spring 配置文件路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/spring-*.xml</param-value></context-param><!-- 配置spring 监听器,监听spring 启动 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- 指定log4j 日志 --><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:config/log4j.xml</param-value></context-param><context-param><param-name>log4jRefreshInterval</param-name><param-value>60000</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>   <!--  所有请求都会先经过cxf框架 -->   <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>/*</url-pattern>    </servlet-mapping></web-app>

【3. 配置spring-cxf.xml】

      1. 引入CXF 默认配置

      2. 配置要发布的WS服务

<?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"><!-- 引入CXF 相关配置 --><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" /><!-- 配置日志入拦截器 --><bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" /><!-- 配置日志出拦截器 --><bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /><!--implementor:指定接口具体实现类,address:随意配 --><jaxws:endpoint id="helloWorldWS" implementor="org.zgf.cxf.spring.ws.HelloWordWS" address="/HelloWorld"><!-- 输入日志拦截器 ,多个日志拦截器按顺序写即可--><jaxws:inInterceptors><ref bean="loggingInInterceptor" /><!-- 也可使用匿名Bean方式配置  <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />--></jaxws:inInterceptors><!-- 输出日志拦截器 --><jaxws:outInterceptors><ref bean="loggingOutInterceptor" /></jaxws:outInterceptors></jaxws:endpoint></beans>


【4. 启动web 服务】

      启动web 服务,在浏览器中输入http://ip:port/projectname (http://localhost:8888/CXF-spring/)可以看到如下页面

   

【5. 客户端测试】

      客户端测试可参看《(三)CXF客户端调用WS服务》


【6. 总结】

     1. 服务器端配置也可以使用<cxf:bus> 标签做全局配置,配置拦截器,详细用法参看《(八)CXF整合spring--调用WS服务》

     2. CXF应用于WEB项目中,导入jar包时,不需要引入jettry的相关jar包,同样在开启GZIP的时候也不用靠CXF来开启, 而是依赖于部署的服务器。 如果WEB服务器开启了GZIP 那么, 如果配置的压缩条件包含text/xml 的话,那么就会自动压缩,因为WS服务返回的内容格式为:Content-Type--text/xml;charset=UTF-8。

    3. 源代码下载:《CXF-spring-server.zip》  



0 0
原创粉丝点击