CXF配置的方式发布WebService并运行于Tomcat容器
前面已经用CXF编码的方式发布了一个WebService,和JDK发布WebService大同小异?。既然CXF已经和Spring集成了、为什么不用Sprnig来配置服务呢? 下面就介绍如何使用Spring配置的方式来发布一个WebService。 新建一个Web Project ,导入CXF所用的包。 返回数据的实体Bean package com.service.cxf.config; /** * @author jackphang * @date 2013-4-14 * @description */ public class HiBean { private int id; private String name; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
? 接口类IHiServiceForCXFConfig ? package com.service.cxf.config; import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.soap.SOAPBinding; /** * @author jackphang * @date 2013-4-14 * @description */ // 必须加此注解、否则所有的方法将不对外暴露 @WebService // 将SOAP的协议提升为1.2版本 @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) public interface IHiServiceForCXFConfig { public String sayHi(String str); public HiBean findHiById(int id); }
? package com.service.cxf.config; /** * @author jackphang * @date 2013-4-14 * @description */ public class HiServiceImplForCXFConfig implements IHiServiceForCXFConfig { @Override public String sayHi(String str) { System.out.println("sayHi被调用,客户端发来数据:" + str); return "hi," + str; } /** * 返回一个实体HiBean */ @Override public HiBean findHiById(int id) { HiBean bean = new HiBean(); bean.setId(id); bean.setName("jack"); bean.setAddress("湖南"); return bean; } }
? <?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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd 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" /> <!-- 发布一个简单服务(不带接口类型),address:请求的url, implementor:被发布的类,需要加注解@WebService,否则所有的方法将不对外暴露,亦不报错 --> <!-- <jaxws:endpoint id="helloServiceCXFForConfig" implementor="com.service.cxf.config.HelloServiceForCXFConfig" address="/hello"> 请求消息拦截器 <jaxws:inInterceptors> <bean id="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> 相应消息拦截器 <jaxws:outInterceptors> <bean id="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:endpoint> --> <!-- 第二种方式发布(带接口类型) --> <!-- serviceClass:必须是接口类型,该接口类型必须加注解@WebService address:请求的url --> <jaxws:server id="hiServiceCXFForConfig" serviceClass="com.service.cxf.config.IHiServiceForCXFConfig" address="/hi"> <!-- serviceBean:服务实例,必须是serviceClass的实现类 --> <jaxws:serviceBean> <bean class="com.service.cxf.config.HiServiceImplForCXFConfig"></bean> </jaxws:serviceBean> <!-- 请求消息拦截器 --> <jaxws:inInterceptors> <bean id="inInterceptor2" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <!-- 相应消息拦截器 --> <jaxws:outInterceptors> <bean id="outInterceptor2" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:server> </beans> 由于我们要运行在Tomcat容器中,所以, 配置完后、就要配置web.xml ,因为cxf-servlet.xml本质就是spring的配置文件,所以在web.xml中的配置和spring的配置方式是一样的。唯一不同的是、我们还要配置一个CXFServlet,由它来访问我们的WebService的服务。 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>CXF Application</display-name> <!-- 项目启动时加载cxf配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/service/cxf/config/cxf-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <!-- CXF核心Servlet --> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <!-- 指定cxf配置文件的路径 ,交给Spring来加载--> <!-- <init-param> <param-name>config-location</param-name> <param-value>classpath:com/service/cxf/config/cxf-servlet.xml</param-value> </init-param> --> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping> </web-app> 配置完成,将项目部署到tomcat,启动tomcat,然后在浏览器端访问http://localhost:8080/WSService/cxf? 就会看到如下页面: 点击相应的WSDL的链接、你就可以看到WSDL说明文档了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |