首页 > Java开发 > cxf与Spring集成webservice

cxf与Spring集成webservice

一、导入jar,在上一篇中我已经写了要导入的jar,有兴趣的读者可以过去看下,

二、编写Spring配置文件applicationContext.xml

[html][/html] view plaincopy

  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="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.    <span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;"> <span style=""><!--
  9.         ***注意***
  10.         手动添加的内容:
  11.         xmlns:jaxws="http://cxf.apache.org/jaxws"
  12.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
  13.      -->
  14. </span></span></span>
  15.     <!-- 引入CXF Bean定义如下 -->
  16.     <import resource="classpath:META-INF/cxf/cxf.xml" />
  17.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  18.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  19. </beans>

三、在web.xml中配置Spring的加载文件和cxf拦截器

[html][/html] view plaincopy

  1. <!-- 加载Spring配置文件 -->
  2.     <context-param>
  3.         <param-name>contextConfigLocation</param-name>
  4.         <param-value>classpath:applicationContext.xml</param-value>
  5.     </context-param>
  6.     <listener>
  7.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8.     </listener>
  9.     <!-- 配置CXF配置文件 -->
  10.     <servlet>
  11.         <servlet-name>cxf</servlet-name>
  12.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  13.         <load-on-startup>1</load-on-startup>
  14.     </servlet>
  15.     <servlet-mapping>
  16.         <servlet-name>cxf</servlet-name>
  17.         <url-pattern>/ws/*</url-pattern><!-- 所有的webservice发布在ws目录下 -->
  18.     </servlet-mapping>

四、编写要提供的webservice接口和实现类

[html][/html] view plaincopy

  1. 2.编写服务接口
  2. @WebService
  3. public interface CxfServiceImpl {
  4.     @WebMethod
  5.     public abstract String getSay(String name);
  6. }
  7. 3.编写服务具体类
  8. @WebService
  9. public class CxfService implements CxfServiceImpl {
  10.     /* (non-Javadoc)
  11.      * @see cn.com.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String)
  12.      */
  13.     @WebMethod
  14.     public String getSay(String name) {
  15.         return "say:" + name + "时间:" + new Date();
  16.     }
  17. }

五、在Spring配置文件中配置接口

[html][/html] view plaincopy

  1. <!-- 配置cxf服务 -->
  2.     <jaxws:server id="say" address="/wss" serviceClass="cn.com.liveuc.cxf.service.CxfServiceImpl">
  3.         <!-- 配置服务类 -->
  4.         <jaxws:serviceBean>
  5.             <bean class="cn.com.liveuc.cxf.service.CxfService"></bean>
  6.         </jaxws:serviceBean>
  7.         <!-- 配置拦截类,获取头信息 -->
  8.         <jaxws:inInterceptors>
  9.             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  10.         </jaxws:inInterceptors>
  11.         <jaxws:outInterceptors>
  12.             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  13.         </jaxws:outInterceptors>
  14.     </jaxws:server>

六、启动tomcat,接口创建成功


本文固定链接: http://www.devba.com/index.php/archives/2088.html | 开发吧

报歉!评论已关闭.