如何压缩Web Service数据
在现实应用中有些时候会有比较大的数据对象需要传输,或者在一个比较慢的.网络环境下发布调用web service,此时可以通过压缩数据流的方式来减小数据包的大小,从而提高web service的性能。下面yjbys小编为大家准备了关于如何压缩Web Service数据的文章,欢迎阅读。
1. 首先模拟一个可以存放大数据的pojo对象,这个对象可以通过构造参数给定的size来模拟一个size大小的字符串。
package com.googlecode.garbagecan.cxfstudy.compress;
public class BigData {
private String name;
private String data;
public BigData() {
}
public BigData(String name, int size) {
this.name = name;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append("0");
}
this.data = sb.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
2. Web Service接口类,和普通的接口定义没有什么区别。
package com.googlecode.garbagecan.cxfstudy.compress;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface BigDataService {
@WebMethod
@WebResult BigData getBigData(@WebParam String name, @WebParam int size);
}
3. Web Service实现类
package com.googlecode.garbagecan.cxfstudy.compress;
public class BigDataServiceImpl implements BigDataService {
public BigData getBigData(String name, int size) {
BigData bigData = new BigData(name, size);
return bigData;
}
}
4. 测试类,这片文章使用了JUnit测试类来做测试。setUpBeforeClass方法用来启动Service, testGetBigData方法用来测试web service。
注意setUpBeforeClass方法中的
factoryBean.getInInterceptors().add(new GZIPInInterceptor());
factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());
和testGetBigData方法中的
endpoint.getInInterceptors().add(new GZIPInInterceptor());
endpoint.getOutInterceptors().add(new GZIPOutInterceptor());
上面两段代码就是告诉CXF使用压缩Interceptor来压缩和解压缩数据包。
package com.googlecode.garbagecan.cxfstudy.compress;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.http.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.http.gzip.GZIPOutInterceptor;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class BigDataServiceTest {
private static final String address = "http://localhost:9000/ws/compress/bigDataService";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.getInInterceptors().add(new GZIPInInterceptor());
factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());
factoryBean.setServiceClass(BigDataServiceImpl.class);
factoryBean.setAddress(address);
factoryBean.create();
}
@Test
public void testGetBigData() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(address);
factoryBean.setServiceClass(BigDataService.class);
Object obj = factoryBean.create();
Client client = ClientProxy.getClient(obj);
Endpoint endpoint = client.getEndpoint();
endpoint.getInInterceptors().add(new GZIPInInterceptor());
endpoint.getOutInterceptors().add(new GZIPOutInterceptor());
BigDataService service = (BigDataService) obj;
Assert.assertNotNull(service);
String name = "my big data";
int size = 1024 * 1024 * 10;
long start = System.currentTimeMillis();
BigData bigData = service.getBigData(name, size);
long stop = System.currentTimeMillis();
System.out.println("Time: " + (stop - start));
Assert.assertNotNull(bigData);
Assert.assertEquals(name, bigData.getName());
Assert.assertEquals(size, bigData.getData().length());
}
}
5. 运行此unit test,可以在日志中看到数据包前后大小和内容。
【如何压缩Web Service数据】相关文章:
如何使用Web Service传输文件12-10
如何创建安全的Web Service12-10
j2ee培训:如何构建RESTful Web Service10-10
C#开发和调用Web Service实例12-06
Web Service的开发与应用基础12-06
Web数据挖掘技术探析11-14
web数据挖掘技术分析与研究11-12
Flash Web结构之数据显示层介绍11-10
如何面试Web前端开发12-12