简体   繁体   English

如何在java中发送响应时压缩Rest服务中的数据(对象)

[英]How to compress data (object) in Rest service during sending responce in java

my piece of code is 我的代码是

  @POST
  @Path("/getJSONCompareData")
  @Produces(MediaType.APPLICATION_JSON)
  public Object getJSONCompareData(@FormParam("testRunNum") String TRun,@FormParam("rptName") String reportName) {

   if(debugLevel>2)
      Log.debugLog(className, "getJSONStatsData", "", "", "Methad called");
      Object obj=null;
      try {
      int testRunNum = Integer.parseInt(TRun);
      String rptName = reportName;
      JSONParser parser=new JSONParser();
      String statsReportNameDir = Config.getWorkPath() + "/webapps/logs/TR" + testRunNum + "/reports/compareReports/" + rptName + "/compare.report";
                    obj=parser.parse(new FileReader(statsReportNameDir));
    }catch(Exception e){
      e.printStackTrace();
      Log.stackTraceLog(className, "getJSONStatsData", "", "", "Exception - ", e);
     }
          return obj;
   }

file size i am reading is approx 10MB 我正在阅读的文件大小约为10MB

i want to compress data as it is taking 1 min over network 我想压缩数据,因为它需要1分钟的时间通过网络

How i compress my Obj and handle in ajax call 我如何压缩我的Obj并在ajax调用中处理

You can compress JSON response using GZIP configuration on server. 您可以使用服务器上的GZIP配置压缩JSON响应。
Modern browsers supports gzip compressed content also Servers like Apache, Tomcat, JBoss etc supports gzip compression too. 现代浏览器也支持gzip压缩内容,Apache,Tomcat,JBoss等服务器也支持gzip压缩。

Hence if gzip is enabled in server, it compress's data then sends to client. 因此,如果在服务器中启用gzip,它会压缩数据然后发送到客户端。
you can refer article http://viralpatel.net/blogs/enable-gzip-compression-in-tomcat/ 你可以参考文章http://viralpatel.net/blogs/enable-gzip-compression-in-tomcat/

GZIP compression will increase performance not only for js,jsp files but also for http requests as data will be compressed GZIP压缩不仅可以提高js,jsp文件的性能,还可以提高http请求的性能,因为数据将被压缩

For compress data in Rest Service you have add(create) an annotation I have create an annotation name ** @Compress ** and implemented code for that on my server 对于Rest Service中的压缩数据,你已经添加(创建)一个注释我已经创建了一个注释名称** @Compress **并在我的服务器上实现了该注释的代码

import com.cavisson.gui.server.webdashboard.services.interceptors.Compress; import com.cavisson.gui.server.webdashboard.services.interceptors.Compress;

here is my class for compression 这是我的压缩类

package com.cavisson.gui.server.webdashboard.services.interceptors;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.ws.rs.NameBinding;

    /**
     * @Compress annotation is the name binding annotation.
     */
    @NameBinding
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Compress {}

now i have bind this interface with my class GZIPWriterInterceptor withuse at rum time 现在我已经在朗姆酒时间使用我的类GZIPWriterInterceptor绑定了这个接口

package com.cavisson.gui.server.webdashboard.services.interceptors;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;

/**
 * This class is used for compressing data sending from server.
 */
@Provider
@Compress
public class GZIPWriterInterceptor implements WriterInterceptor
{
  @Override
  public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException
  {
    try
    {
      MultivaluedMap<String,Object> headers = context.getHeaders();
      headers.add("Content-Encoding", "gzip");

      final OutputStream outputStream = context.getOutputStream();
      context.setOutputStream(new GZIPOutputStream(outputStream));
      context.proceed();
    }
    catch(IOException | WebApplicationException  e)
    {
      e.printStackTrace();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

Heading ## 标题##

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM