GZIP在PlayFramework 2.0中的响应主体

GZIP在PlayFramework 2.0中的响应主体,playframework,playframework-2.0,gzip,Playframework,Playframework 2.0,Gzip,我正在开发PlayFramework2.x应用程序。我的应用程序中的控制器将JSON响应返回给浏览器/端点。我想知道是否有一种简单的方法来实现响应体的GZIP压缩 目前在游戏2.0.4中,对于非资产没有简单的方法 对于Java API,您可以使用: public static Result actionWithGzippedJsonResult() throws IOException { Map<String, String> map = new HashMap<St

我正在开发PlayFramework2.x应用程序。我的应用程序中的控制器将JSON响应返回给浏览器/端点。我想知道是否有一种简单的方法来实现响应体的GZIP压缩

目前在游戏2.0.4中,对于非资产没有简单的方法

对于Java API,您可以使用:

public static Result actionWithGzippedJsonResult() throws IOException {
    Map<String, String> map = new HashMap<String, String>();
    map.put("hello", "world");
    final String json = Json.toJson(map).toString();
    return gzippedOk(json).as("application/json");
}

/** Creates a response with a gzipped string. Does NOT change the content-type. */
public static Results.Status gzippedOk(final String body) throws IOException {
    final ByteArrayOutputStream gzip = gzip(body);
    response().setHeader("Content-Encoding", "gzip");
    response().setHeader("Content-Length", gzip.size() + "");
    return ok(gzip.toByteArray());
}

//solution from James Ward for Play 1 and every request: https://gist.github.com/1317626
public static ByteArrayOutputStream gzip(final String input)
        throws IOException {
    final InputStream inputStream = new ByteArrayInputStream(input.getBytes());
    final ByteArrayOutputStream stringOutputStream = new ByteArrayOutputStream((int) (input.length() * 0.75));
    final OutputStream gzipOutputStream = new GZIPOutputStream(stringOutputStream);

    final byte[] buf = new byte[5000];
    int len;
    while ((len = inputStream.read(buf)) > 0) {
        gzipOutputStream.write(buf, 0, len);
    }

    inputStream.close();
    gzipOutputStream.close();

    return stringOutputStream;
}
publicstaticresult actionWithGzippedJsonResult()引发IOException{
Map Map=newhashmap();
地图。放置(“你好”,“世界”);
最后一个字符串json=json.toJson(map.toString();
返回gzippedOk(json).as(“application/json”);
}
/**使用gzip字符串创建响应。不更改内容类型*/
公共静态结果。状态gzip(最终字符串体)引发IOException{
最终ByteArrayOutputStream gzip=gzip(主体);
response().setHeader(“内容编码”、“gzip”);
response().setHeader(“内容长度”,gzip.size()+”);
返回ok(gzip.toByteArray());
}
//James Ward针对第1场比赛和每个请求的解决方案:https://gist.github.com/1317626
公共静态ByteArrayOutputStream gzip(最终字符串输入)
抛出IOException{
final InputStream InputStream=new ByteArrayInputStream(input.getBytes());
final ByteArrayOutputStream stringOutputStream=new ByteArrayOutputStream((int)(input.length()*0.75));
最终输出流gzipOutputStream=新的gzipOutputStream(stringOutputStream);
最终字节[]buf=新字节[5000];
内伦;
而((len=inputStream.read(buf))>0){
写入(buf,0,len);
}
inputStream.close();
gzipOutputStream.close();
返回stringOutputStream;
}

gzip'ing拥有Apache前端,几乎是一块完整的蛋糕

在Apache 2.4 gzip上,使用一组基本的内容类型通过
Location
block进行处理可能如下所示:

<Location />
  ...
  AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon
  SetOutputFilter DEFLATE
</Location>

...
AddOutputFilterByType DEFLATE text/css应用程序/x-javascript文本/x-component文本/html文本/richtext图像/svg+xml文本/纯文本/xsd文本/xsl文本/xml图像/x-icon
SetOutputFilter放气

播放框架2.2+
中可以使用。可通过sbt获得:

libraryDependencies ++= filters

如果你是scala人,那么值得一看。

播放2.5,如前所述:

下面是包含gZip过滤器的示例代码(以及用于展示添加多个过滤器的示例CORS过滤器):


如果Play生活在前端(apache、nginx等)之后,那么就在那里这样做,简单、直接、有效如果你把这作为一个答案,我会接受:)当然,我会包括我的方法,第
import javax.inject.Inject;

import play.api.mvc.EssentialFilter;
import play.filters.cors.CORSFilter;
import play.filters.gzip.GzipFilter;
import play.http.HttpFilters;


public class Filters implements HttpFilters {

    @Inject
    CORSFilter corsFilter;

    @Inject
    GzipFilter gzipFilter;

    @Override
    public EssentialFilter[] filters() {
        return new EssentialFilter[] { corsFilter, gzipFilter };
    }

}