(八) CXF 整合Spring--调用WS服务

来源:互联网 发布:数控车床编程g代码 编辑:程序博客网 时间:2024/06/05 22:51

    使用Spring + CXF 调用WS服务,将更加简单。


【1. 构建服务器端】

      此例子的服务器端构建,设置服务端支持GZIP 压缩。 请参考《(六)CXF 客户端调用--GZIP压缩》


【2. 客户端构建】

      此处使用普通的Java Project ,而不是JavaWeb Project。


【2.1 引入jar包】

     1. 除了引入spring 的相关jar 包之外,还需要引入cxf 的相关jar包。 cxf引入包参考《(七)CXF整合Spring--发布WS服务》

     2. 最终包结构为:

       


【2. 2 生成客户端】

        1. 使用wsdl2java 命令生成客户端, 请参考《(三)CXF客户端调用WS服务》

 

【2.3 配置方式一】

 配置文件:spring-cxf.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:http-conf="http://cxf.apache.org/transports/http/configuration"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans.xsd                http://cxf.apache.org/transports/http/configuration                http://cxf.apache.org/schemas/configuration/http-conf.xsd                http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd               http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!-- 设置 Http 请求的相关信息--><http-conf:conduit name="*.http-conduit"><!-- 设置 GZIP 压缩头, 响应超时等参数--><http-conf:client AcceptEncoding="gzip" ConnectionTimeout="30000"  /></http-conf:conduit><!-- 配置cxf 提供的入拦截器  --><bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/><!-- 配置cxf 提供的出拦截器   --><bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><!-- 配置WS 服务客户端:    serviceClass:WS服务的接口代理,是一个接口,所以不能用普通的bean标签直接配置    address:WS服务地址 --><jaxws:client id="helloWorldWS" serviceClass="org.zgf.cxf.spring.ws.IHelloWorldWS" address="http://172.22.12.85:8180/ws/hellowordws"><!-- 配置此WS服务特有的特性--><jaxws:features><!-- 配置GZIP 压缩特性 --><bean class="org.apache.cxf.transport.common.gzip.GZIPFeature"><property name="threshold" value="1" /></bean></jaxws:features><!-- 配置入拦截器:只适用于这一个bean实例 --><jaxws:inInterceptors><ref bean="loggingInInterceptor"/></jaxws:inInterceptors><!-- 配置出拦截器:只使用与这一个bean实例 --><jaxws:outInterceptors><ref bean="loggingOutInterceptor"/></jaxws:outInterceptors></jaxws:client><!-- 配置第二个实例,不启用GZIP 压缩   --><jaxws:client id="helloWorldWS2" serviceClass="org.zgf.cxf.spring.ws.IHelloWorldWS" address="http://172.22.12.85:8180/ws/hellowordws"/></beans>

测试用例:Test_client.java
package org.zgf.cxf.spring.ws;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 测试用例 */public class Test_client {public static void main(String[] args) {//1. 初始化spring 容器ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext("classpath*:spring-cxf.xml");test_1(context); //正常运行,因为配置了gzip 特性 //test_2(context); //抛出异常,因为没有配置gzip 特性}private static void test_1(ClassPathXmlApplicationContext context){//2. 从spring 容器中获取WS服务接口代理对象IHelloWorldWS helloWorldWS = (IHelloWorldWS) context.getBean("helloWorldWS");//3. 调用接口服务方法String result = helloWorldWS.sayByeBye("zonasdfasdfasdfasdfasdg");System.out.println("result:" + result);}private static void test_2(ClassPathXmlApplicationContext context){//2. 从spring 容器中获取WS服务接口代理对象IHelloWorldWS helloWorldWS = (IHelloWorldWS) context.getBean("helloWorldWS2");//3. 调用接口服务方法String result = helloWorldWS.sayByeBye("zonasdfasdfasdfasdfasdg");System.out.println("result:" + result);}}

【2.3 配置方式二】

配置文件:spring-cxf2.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:http-conf="http://cxf.apache.org/transports/http/configuration"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans.xsd                http://cxf.apache.org/transports/http/configuration                http://cxf.apache.org/schemas/configuration/http-conf.xsd                http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd               http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!-- 设置 Http 请求的相关信息 --><http-conf:conduit name="*.http-conduit"><!-- 设置 GZIP 压缩头, 响应超时等参数 --><!-- 设置gzip 是为了告知服务器端,返回gzip压缩后的数据 --><http-conf:client AcceptEncoding="gzip"ConnectionTimeout="30000" /></http-conf:conduit><!-- 配置两个WS 服务实例 --><jaxws:client id="helloWorldWS" serviceClass="org.zgf.cxf.spring.ws.IHelloWorldWS" address="http://172.22.12.85:8180/ws/hellowordws"/><jaxws:client id="helloWorldWS2" serviceClass="org.zgf.cxf.spring.ws.IHelloWorldWS" address="http://172.22.12.85:8180/ws/hellowordws"/><!-- cxf:bus 配置相当于总体配置,作用域所有通过CXF的客户端。 --><cxf:bus><!-- 配置cxf feature --><cxf:features><!-- 配置GZIPFeature 目的是为了解析服务器端返回的GZIP 数据,如果不配置,返回的数据将不能解析 --><bean class="org.apache.cxf.transport.common.gzip.GZIPFeature"><property name="threshold" value="1" /></bean></cxf:features><!-- 配置日志入拦截器 --><cxf:inInterceptors><bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /></cxf:inInterceptors><!-- 配置出拦截器 --><cxf:outInterceptors><bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /></cxf:outInterceptors></cxf:bus></beans>

测试用例:Test_client2.java

package org.zgf.cxf.spring.ws;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 测试用例 */public class Test_client2 {public static void main(String[] args) {//1. 初始化spring 容器ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext("classpath*:spring-cxf2.xml");//均能正常运行,因为配置了总的GZIP 特性test_1(context);test_2(context); }private static void test_1(ClassPathXmlApplicationContext context){//2. 从spring 容器中获取WS服务接口代理对象IHelloWorldWS helloWorldWS = (IHelloWorldWS) context.getBean("helloWorldWS");//3. 调用接口服务方法String result = helloWorldWS.sayByeBye("zonasdfasdfasdfasdfasdg");System.out.println("result:" + result);}private static void test_2(ClassPathXmlApplicationContext context){//2. 从spring 容器中获取WS服务接口代理对象IHelloWorldWS helloWorldWS = (IHelloWorldWS) context.getBean("helloWorldWS2");//3. 调用接口服务方法String result = helloWorldWS.sayByeBye("zonasdfasdfasdfasdfasdg");System.out.println("result:" + result);}}

【3. 总结】

     1. 客户端引入cxf的jar包,或许不需要那么多,这个笔者就不研究了,如果有兴趣,请自行研究。

     2. 在配置Feature 和 Interceptor 时,可以为每个WS服务接口单独配置,也可以使用<cxf:bus> 标签做整体配置。

     3. 源代码下载:《CXF-Spring-Client-J.zip》.


0 0
原创粉丝点击