谈谈WebService开发-应用篇(四)-CXF基于Spring的应用
2015-05-08 15:23
369 查看
前面介绍的关于WebService的了开发,对于服务器端的服务发布,我们采用的是main方法的方式,这种方式在工作中一般不会用,这种方式也不太优雅!那么下面我们来看下CXF基于Spring的开发过程。
还是以最简单的基础篇里说的SayHello为Demo分解下开发及配置过程:
2.引入CXF及Spring相关的jar包,需要的朋友可以网上自行下载或小窗我,这里就不上传附件了;
3.开发ws服务的SEI及实现类;
SEI:
4.新建Spring配置文件beans.xml内容如下:
2.引入CXF与Spring相关的jar包;
3.通过wsdl2java工具生成客户调用代码,具体方法前面几篇webservice应用中有介绍,这里就不赘述了;
4.配置客户端Spring配置client_beans.xml,配置如下,其中id为一个唯定的beanId,serviceClass表示SEI:
输出结果如下,以下可以看出我们配置的系统拦截器已经生效:
同样的,对于服务器端也支持配置系统拦截器或自定义拦器,用于做些与业务无关的通用处理。
还是以最简单的基础篇里说的SayHello为Demo分解下开发及配置过程:
服务器开发
1.新建服务器端工程 SayHello-cxfspring-Server,此工程类型为Dynamic Web Project;2.引入CXF及Spring相关的jar包,需要的朋友可以网上自行下载或小窗我,这里就不上传附件了;
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; } }
4.新建Spring配置文件beans.xml内容如下:
<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包;
3.通过wsdl2java工具生成客户调用代码,具体方法前面几篇webservice应用中有介绍,这里就不赘述了;
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
同样的,对于服务器端也支持配置系统拦截器或自定义拦器,用于做些与业务无关的通用处理。
相关文章推荐
- 谈谈WebService开发-应用篇(二)-基于CXF-天气预报
- Web应用中使用CXF不基于Spring的WebService发布
- WebService(4):CXF Spring开发WebService,基于SOAP和REST方式
- 基于Spring和CXF的webservice开发环境搭建
- Web应用中使用CXF不基于Spring的WebService发布
- CXF Spring开发WebService,基于SOAP和REST方式
- 基于Spring+CXF技术开发WebService
- CXF Spring开发WebService,基于SOAP和REST方式
- CXF使用教程(三)——基于Spring的webService开发
- webService(CXF开发SOAP应用,CXF整合spring)未完待续
- 基于spring4.3.6,cxf3.0.12的WebService开发
- Spring+CXF开发基于SOAP协议的WebService
- 基于Spring和CXF的webservice开发环境搭建
- 搭建基于CXF的WebService服务器及其应用
- spring+cxf 注解开发webservice
- CXF整合Spring开发WebService
- WebService技术总结(五):CXF整合Spring开发
- Spring+CXF开发WebService
- Webservice的cxf开发_使用spring配置客户端
- WebService-CXF-Spring基于web的cxf