admin管理员组

文章数量:1022982

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/pareReports/" + rptName + "/pare.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

i want to press data as it is taking 1 min over network

How i press my Obj and handle in ajax call

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/pareReports/" + rptName + "/pare.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

i want to press data as it is taking 1 min over network

How i press my Obj and handle in ajax call

Share Improve this question edited Aug 1, 2016 at 4:51 Vikrant Kashyap 6,8963 gold badges34 silver badges53 bronze badges asked Aug 1, 2016 at 4:47 Amit RaiAmit Rai 4071 gold badge4 silver badges22 bronze badges 3
  • I think you should add more details (as tags too) about the framework you use. – Gábor Bakos Commented Aug 1, 2016 at 5:10
  • I am using jersey for implementation rest call and from my js file i am hitting request /dashboardserver/web/ReportWebservice/getJSONCompareData – Amit Rai Commented Aug 1, 2016 at 6:30
  • I am using jersey for implementation rest call and from my js file i am hitting request /dashboardserver/web/ReportWebservice/getJSONCompareData which call getJSONCompareData method here i am reading a file whose size is approx 10MB send as repose to my js file. I want to press jSon object and send over network in responce if rest – Amit Rai Commented Aug 1, 2016 at 8:32
Add a ment  | 

2 Answers 2

Reset to default 3

You can press JSON response using GZIP configuration on server.
Modern browsers supports gzip pressed content also Servers like Apache, Tomcat, JBoss etc supports gzip pression too.

Hence if gzip is enabled in server, it press's data then sends to client.
you can refer article http://viralpatel/blogs/enable-gzip-pression-in-tomcat/

GZIP pression will increase performance not only for js,jsp files but also for http requests as data will be pressed

For press 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

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

here is my class for pression

package .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

package .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 pressing 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();
    }
  }
}

              

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/pareReports/" + rptName + "/pare.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

i want to press data as it is taking 1 min over network

How i press my Obj and handle in ajax call

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/pareReports/" + rptName + "/pare.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

i want to press data as it is taking 1 min over network

How i press my Obj and handle in ajax call

Share Improve this question edited Aug 1, 2016 at 4:51 Vikrant Kashyap 6,8963 gold badges34 silver badges53 bronze badges asked Aug 1, 2016 at 4:47 Amit RaiAmit Rai 4071 gold badge4 silver badges22 bronze badges 3
  • I think you should add more details (as tags too) about the framework you use. – Gábor Bakos Commented Aug 1, 2016 at 5:10
  • I am using jersey for implementation rest call and from my js file i am hitting request /dashboardserver/web/ReportWebservice/getJSONCompareData – Amit Rai Commented Aug 1, 2016 at 6:30
  • I am using jersey for implementation rest call and from my js file i am hitting request /dashboardserver/web/ReportWebservice/getJSONCompareData which call getJSONCompareData method here i am reading a file whose size is approx 10MB send as repose to my js file. I want to press jSon object and send over network in responce if rest – Amit Rai Commented Aug 1, 2016 at 8:32
Add a ment  | 

2 Answers 2

Reset to default 3

You can press JSON response using GZIP configuration on server.
Modern browsers supports gzip pressed content also Servers like Apache, Tomcat, JBoss etc supports gzip pression too.

Hence if gzip is enabled in server, it press's data then sends to client.
you can refer article http://viralpatel/blogs/enable-gzip-pression-in-tomcat/

GZIP pression will increase performance not only for js,jsp files but also for http requests as data will be pressed

For press 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

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

here is my class for pression

package .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

package .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 pressing 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();
    }
  }
}

              

本文标签: javascriptHow to compress data (object) in Rest service during sending responce in javaStack Overflow