前面介绍的关于WebService的了开发,对于服务器端的服务发布,我们采用的是main方法的方式,这种方式在工作中一般不会用,这种方式也不太优雅!那么下面我们来看下CXF基于Spring的开发过程。
还是以最简单的基础篇里说的SayHello为Demo分解下开发及配置过程:
服务器开发
1.新建服务器端工程 SayHello-cxfspring-Server,此工程类型为Dynamic Web Project;
3.开发ws服务的SEI及实现类;
SEI:
package com.devins.ws.server.service; import javax.jws.WebMethod; import javax.jws.WebService; /** * * SEI定义 * */ @WebService public interface ISayHello { @WebMethod public String sayHello(String name); }实现类:
package com.devins.ws.server.service.impl; import javax.jws.WebService; import com.devins.ws.server.service.ISayHello; /** * SEI实现 */ @WebService public class SayHelloImpl implements ISayHello{ @Override public String sayHello(String name) { // Todo Auto-generated method stub System.out.println("Server: Hello! "+name); return "Hello: "+name; } }
<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" /> <!-- 发布服务配置 --> <jaxws:endpoint id="sayHello" implementor="com.devins.ws.server.service.impl.SayHelloImpl" address="/sayHello" /> </beans>5.至此,服务器端CXF基于Spring的开发已经完成,接下与其它web工程类似发布工程到tomcat,启动即可,启动后运行结果如下:
客户端开发
1.创建客户端工程 SayHello-cxfspring-Client;
2.引入CXF与Spring相关的jar包;
4.配置客户端Spring配置client_beans.xml,配置如下,其中id为一个唯定的beanId,serviceClass表示SEI:
<?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"> <jaxws:client id="sayHelloClient" serviceClass="com.devins.ws.server.service.ISayHello" address="http://localhost:8087/SayHello-cxfspring-Server/sayHello" > </jaxws:client> </beans>5.针对第4步的配置,CXF支持配置拦截器,这里我们尝试配置两个系统拦截器,一个入拦截器,一个出拦截器用于记录我们的输入输出;CXF也支持自定义拦截器,这里就不演示了,有需要的话,后面专门来一篇介绍拦截器的开发步骤。
<?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"> <jaxws:client id="sayHelloClient" serviceClass="com.devins.ws.server.service.ISayHello" address="http://localhost:8087/SayHello-cxfspring-Server/sayHello" > <span style="color:#ff0000;"><jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors></span> </jaxws:client> </beans>6.编写测试类,客户端就直接用一个main方法来调用模拟下:
package com.devins.ws.client; import org.springframework.context.support.ClasspathXmlApplicationContext; import com.devins.ws.server.service.ISayHello; public class Client { public Client() { // Todo Auto-generated constructor stub } public static void main(String[] args) { ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext( new String[] { "client_beans.xml" }); ISayHello bean = (ISayHello)context.getBean("sayHelloClient"); String hello = bean.sayHello("dong"); System.out.println(hello); } }
输出结果如下,以下可以看出我们配置的系统拦截器已经生效:
2015-5-12 11:17:04 org.apache.cxf.services.ISayHelloService.ISayHelloPort.ISayHello <span style="color:#3333ff;">信息: Outbound Message</span> --------------------------- ID: 1 Address: http://localhost:8087/SayHello-cxfspring-Server/sayHello Encoding: UTF-8 Content-Type: text/xml Headers: {Accept=[*/*],SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://service.server.ws.devins.com/"><arg0>dong</arg0></ns2:sayHello></soap:Body></soap:Envelope> -------------------------------------- 2015-5-12 11:17:04 org.apache.cxf.services.ISayHelloService.ISayHelloPort.ISayHello <span style="color:#3333ff;">信息: Inbound Message</span> ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {Content-Length=[231],content-type=[text/xml;charset=UTF-8],Date=[Tue,12 May 2015 03:17:04 GMT],Server=[Apache-Coyote/1.1]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://service.server.ws.devins.com/"><return>Hello: dong</return></ns2:sayHelloResponse></soap:Body></soap:Envelope> -------------------------------------- Hello: dong同样的,对于服务器端也支持配置系统拦截器或自定义拦器,用于做些与业务无关的通用处理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。