当前位置:首页 > 主打产品 >

二、cxf与Spring集成webservice

作者:永年县聚联紧固件制造有限公司    发布时间:2017-09-07 13:29:10

二、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 interface 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 interface 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,接口创建成功

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:十堰网站制作 http://shiyan.666rj.com

  • 上一篇:如何正确描述网站title关键词
  • 下一篇:最后一页
  •