cxf数据压缩
一、HTTP數據的壓縮
在http協議中當content-encoding對應的值為gzip,deflate,x-gzip,x-deflate時,數據是經過了壓縮之后再進行傳輸的。有些時候我們當我們傳輸的數據比較大的時候,可以采取這種方式,從而提高數據的傳輸速度。在web service中,當某個接口返回的數據比較大的時候,也可以進行壓縮處理。
二、示例
1、使用soap協議的web service
實體類:
package com.cxf.compress.ws;public class Person {private String name;private String homeAddress;private String companyAddress;public Person() {StringBuilder sb = new StringBuilder();for (int i = 1; i < 10; ++i) {sb.append("X");}this.name = sb.toString();this.homeAddress = sb.toString();this.companyAddress = sb.toString();}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getHomeAddress() {return homeAddress;}public void setHomeAddress(String homeAddress) {this.homeAddress = homeAddress;}public String getCompanyAddress() {return companyAddress;}public void setCompanyAddress(String companyAddress) {this.companyAddress = companyAddress;} }接口MyService:
package com.cxf.compress.ws; import java.util.List;import javax.jws.WebMethod; import javax.jws.WebResult; import javax.jws.WebService;@WebService public interface MyService{@WebMethod@WebResult(name = "personResult")public List<Person> getPerson(); }實現類MyServiceImpl:
package com.cxf.compress.ws; import java.util.ArrayList; import java.util.List;public class MyServiceImpl implements MyService {@Overridepublic List<Person> getPerson() {List<Person> list = new ArrayList<Person>();list.add(new Person());list.add(new Person());list.add(new Person());list.add(new Person());return list;} }服務端啟動類:
package com.cxf.compress.ws;import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import org.apache.cxf.transport.common.gzip.GZIPInInterceptor; import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor; /*** 使用數據壓縮**/ public class MyServer {public static void main(String[] args) {JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();factoryBean.getInInterceptors().add(new GZIPInInterceptor());//解壓使用 gzip的請求factoryBean.getInInterceptors().add(new LoggingInInterceptor());GZIPOutInterceptor out = new GZIPOutInterceptor();out.setThreshold(0);//壓縮數據的閥值設置為0, 默認超過1k的數據才使用gzip壓縮,設置為0,表示響應的數據只要大于0就進行壓縮處理 factoryBean.getOutInterceptors().add(out);//返回的數據使用gizp壓縮factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());String address = "http://localhost:8080/cxf/myservice";factoryBean.setAddress(address);factoryBean.setServiceClass(MyServiceImpl.class);factoryBean.create();} }客戶端請求類:
package com.cxf.compress.ws;import java.util.List;import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.transport.common.gzip.GZIPInInterceptor; import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;public class MyClient {public static void main(String[] args) {JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();factoryBean.setAddress("http://localhost:8080/cxf/myservice");factoryBean.setServiceClass(MyService.class);Object object = factoryBean.create();Client client = ClientProxy.getClient(object);Endpoint endpoint = client.getEndpoint();endpoint.getInInterceptors().add(new GZIPInInterceptor());//解壓使用 gzip的請求 GZIPOutInterceptor out = new GZIPOutInterceptor();out.setThreshold(0);//壓縮數據的閥值設置為0, 默認超過1k的數據才使用gzip壓縮,設置為0,表示響應的數據只要大于0就進行壓縮處理 endpoint.getOutInterceptors().add(out);//返回的數據使用gizp壓縮 MyService service = (MyService) object;List<Person> list = service.getPerson();System.out.println("name: " + list.get(0).getName());} }2、restful web service
實體類Person:
package com.cxf.compress.rs;import java.util.Date;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "person") public class Person {private int id;private String name;private Date date;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}服務接口MyService:
package com.cxf.compress.rs;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces;@Path("person") @Produces("*/*") public interface MyService {@GET@Path("/")public java.util.List<Person> getAll();}服務接口實現類MyServiceImpl:
package com.cxf.compress.rs; import java.util.Date; import java.util.List;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces;public class MyServiceImpl implements MyService {@Overridepublic List<Person> getAll() {List<Person> persons = new java.util.ArrayList<Person>();Person person = new Person();person.setId(111);person.setName("zhangsan");person.setDate(new Date());Person person2 = new Person();person2.setId(222);person2.setName("lisi");person2.setDate(new Date());persons.add(person);persons.add(person2);return persons;}}服務類Server:
package com.cxf.compress.rs;import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.transport.common.gzip.GZIPInInterceptor; import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;public class Server {public static void main(String[] args) {JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();factoryBean.setAddress("http://localhost:8080/myservice");factoryBean.setResourceClasses(MyServiceImpl.class);factoryBean.getInInterceptors().add(new GZIPInInterceptor());//解壓使用 gzip的請求factoryBean.getInInterceptors().add(new LoggingInInterceptor());GZIPOutInterceptor out = new GZIPOutInterceptor();out.setThreshold(0);//壓縮數據的閥值設置為0, 默認超過1k的數據才使用gzip壓縮,設置為0,表示響應的數據只要大于0就進行壓縮處理 factoryBean.getOutInterceptors().add(out);//返回的數據使用gizp壓縮factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());factoryBean.create();} }客戶端訪問類Client:
package com.cxf.compress.rs;import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.io.CachedOutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element;public class Client {public static void main(String[] args) throws Exception {String getResult = get("http://localhost:8080/myservice/person");System.out.println(getResult);}private static String get(String url) throws IOException,ParserConfigurationException {HttpGet get = new HttpGet(url);get.setHeader("Accept", "application/json");get.addHeader("Accept-Encoding" ,"gzip"); //請求使用數據壓縮CloseableHttpClient client = HttpClients.createDefault();String responseContent = null;CloseableHttpResponse response = null;try {response = client.execute(get);HttpEntity entity = response.getEntity();// 響應體if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 響應碼responseContent = EntityUtils.toString(entity, "UTF-8");}} catch (ClientProtocolException e) {e.printStackTrace();}return responseContent;}}對于使用了gzip進行壓縮之后的響應,通過topMon進行監聽后,傳輸的數據大小會小很多。
總結
- 上一篇: 做梦老是梦到前妻是怎么回事
- 下一篇: 简单的创建一个性能计数器