当前位置:编程学习 > 网站相关 >>

二、cxf与Spring集成webservice

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

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

 

?<?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"> 
   <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><SPAN style="COLOR: #ff0000"> <SPAN><!--  
        ***注意***   
        手动添加的内容: 
        xmlns:jaxws="http://cxf.apache.org/jaxws" 
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" 
     --> 
</SPAN></SPAN></SPAN>     
    <!-- 引入CXF Bean定义如下 --> 
    <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" /> 
     
</beans> 

<?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">
    <!--
        ***注意*** 
        手动添加的内容:
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
     -->
   
 <!-- 引入CXF Bean定义如下 -->
 <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" />
 
</beans>

、在web.xml中配置Spring的加载文件和cxf易做图

 

<!-- 加载Spring配置文件 --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <!-- 配置CXF配置文件 --> 
    <servlet> 
        <servlet-name>cxf</servlet-name> 
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>cxf</servlet-name> 
        <url-pattern>/ws/*</url-pattern><!-- 所有的webservice发布在ws目录下 --> 
    </servlet-mapping> 

<!-- 加载Spring配置文件 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 配置CXF配置文件 -->
 <servlet>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/ws/*</url-pattern><!-- 所有的webservice发布在ws目录下 -->
 </servlet-mapping>

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

 

2.编写服务接口 
@WebService 
public inte易做图ce CxfServiceImpl { 
     
    @WebMethod 
    public abstract String getSay(String name); 
 
} 
3.编写服务具体类 
@WebService 
public class CxfService implements CxfServiceImpl { 
     
    /* (non-Javadoc) 
     * @see cn.com.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String) 
     */ 
    @WebMethod 
    public String getSay(String name) { 
        return "say:" + name + "时间:" + new Date(); 
    } 
} 

2.编写服务接口
@WebService
public inte易做图ce CxfServiceImpl {
 
 @WebMethod
 public abstract String getSay(String name);

}
3.编写服务具体类
@WebService
public class CxfService implements CxfServiceImpl {
 
 /* (non-Javadoc)
  * @see cn.com.liveuc.cxf.service.CxfServiceImpl#getSay(java.lang.String)
  */
 @WebMethod
 public String getSay(String name) {
  return "say:" + name + "时间:" + new Date();
 }
}

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

  

配置cxf服务 --> 
    <jaxws:server id="say" address="/wss" serviceClass="cn.com.liveuc.cxf.service.CxfServiceImpl"> 
        <!-- 配置服务类 --> 
        <jaxws:serviceBean> 
            <bean class="cn.com.liveuc.cxf.service.CxfService"></bean> 
        </jaxws:serviceBean> 
        <!-- 配置拦截类,获取头信息 --> 
        <jaxws:inInterceptors> 
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 
        </jaxws:inInterceptors> 
        <jaxws:outInterceptors> 
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 
        </jaxws:outInterceptors> 
    </jaxws:server> 

<!-- 配置cxf服务 -->
 <jaxws:server id="say" address="/wss" serviceClass="cn.com.liveuc.cxf.service.CxfServiceImpl">
  <!-- 配置服务类 -->
  <jaxws:serviceBean>
   <bean class="cn.com.liveuc.cxf.service.CxfService"></bean>
  </jaxws:serviceBean>
  <!-- 配置拦截类,获取头信息 -->
  <jaxws:inInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  </jaxws:inInterceptors>
  <jaxws:outInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  </jaxws:outInterceptors>
 </jaxws:server>六、启动tomcat,接口创建成功

 


 

 

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,