谈谈WebService开发-应用篇(四)-CXF基于Spring的应用
发布时间:2020-12-16 22:52:16 所属栏目:安全 来源:网络整理
导读:前面介绍的关于WebService的了开发,对于服务器端的服务发布,我们采用的是main方法的方式,这种方式在工作中一般不会用,这种方式也不太优雅!那么下面我们来看下CXF基于Spring的开发过程。 ? ? ? ?还是以最简单的基础篇里说的SayHello为Demo分解下开发及配
前面介绍的关于WebService的了开发,对于服务器端的服务发布,我们采用的是main方法的方式,这种方式在工作中一般不会用,这种方式也不太优雅!那么下面我们来看下CXF基于Spring的开发过程。 ? ? ? ?还是以最简单的基础篇里说的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同样的,对于服务器端也支持配置系统拦截器或自定义拦器,用于做些与业务无关的通用处理。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |