This article link: http://blog.csdn.net/kongxx/article/details/7530216
Apache CXF One of the actual combat Hello World Web Service
Apache CXF Combat II Integrated sping and Web container
Apache CXF Three-combat Transfer Java objects
Apache CXF Real-combat four build restful Web Service
In the real world, sometimes there will be large data objects need to be transmitted, or in a relatively slow network environment to publish the call Web service, at this time can be compressed data flow to reduce the size of the packet, thereby improving the performance of the Web service. Let's look at how to do this.
1. First simulate a Pojo object that can hold large data, which can simulate a size string by constructing the size given by the parameter.
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 interface classes are no different from common interface definitions.
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 Implementation Class
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. Test class, this article uses the JUnit test class to do the test. The Setupbeforeclass method is used to start the service and the Testgetbigdata method is used to test the Web service.
Note the Setupbeforeclass method in the
Factorybean.getininterceptors (). Add (New Gzipininterceptor ());
Factorybean.getoutinterceptors (). Add (New Gzipoutinterceptor ());
And in the Testgetbigdata method
Endpoint.getininterceptors (). Add (New Gzipininterceptor ());
Endpoint.getoutinterceptors (). Add (New Gzipoutinterceptor ());
The above two pieces of code tell CXF to use compression interceptor to compress and decompress packets.
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/bigDataSer
Vice "; @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. Run this unit test to see the packet size and content in the log.