在XFire1.2以后支持SOAP消息的GZip压缩传输,在合适的地方启动GZip压缩可以减少网络传输的数据量,加快速度。

文章目录

1. jar下载地址

​https://sourceforge.net/projects/pjl-comp-filter/​

在 xfire中使用 GZip来 压缩 传输量_服务端


或者直接点击下载最新版本:

​https://sourceforge.net/projects/pjl-comp-filter/files/latest/download​

2. 服务端

服务端启动GZip:在服务端启动GZip只需将PJL Compressing Filter下的jar包(用到的)导入到web的/WEB-INF/lib目录下。

并在web.xml文件中增加如下配置:

<filter>
<filter-name>CompressingFilter</filter-name>
<filter-class>
com.planetj.servlet.filter.compression.CompressingFilter
</filter-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>statsEnabled</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CompressingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

服务端加入GZip时web.xml中增加的配置

注:根据web.xml文法定义,这个定义必须位于servlet定义之前。

二、客户端启用GZip:客户端启动GZip只要将客户端的GZip的属性设为true却可。代码如下:

3.客户端

早期:客户端调用的修改(不建议使用)

= "http://192.168.0.225/ldaxfire/services/LDAService";
String namespace = "http://test.yicha.cn/adreport";

// 创建service对象
Service serviceModel = new ObjectServiceFactory().create(ServiceInf.class, null,
namespace,null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
// 获取服务对象
ServiceInf service = (ServiceInf) serviceFactory.create(serviceModel, serviceURL);
// 获取客户端代理
Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
/**
* 此属性作用:开启GZip压缩传输
* 客户端配置属性影响:当服务端没有启用GZip,客户端启用请求GZip压缩时,会产生SOAP解析错误
* 服务端配置属性影响:服务端这个配置不管配不配置(也就是说:如果服务端启动了GZip压缩功能,客户端是否启用GZip都没有影响)
*/
// 启动response压缩
client.setProperty(CommonsHttpMessageSender.GZIP_RESPONSE_ENABLED, Boolean.TRUE);
// 启动request压缩
client.setProperty(CommonsHttpMessageSender.GZIP_RESPONSE_ENABLED,Boolean.TRUE);
// 同时启动response和request压缩
client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,Boolean.TRUE);
// 忽略超时
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
// 调用服务 //这里的nciicCompare是服务端接口 accreditFile是秘钥 reqXML2请求报文
String data = service.nciicCompare("","");

注:在同时启用时,不必再分别启用response和request的GZip压缩。
注意,当服务端没有启用GZip,客户端启用请求GZip压缩时,会产生SOAP解析错误,如果服务端启动了GZip压缩功能,客户端是否启用GZip都没有影响。

推荐企业级xfire搭建
​​​IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务​