简体   繁体   English

GZIP PlayFramework 2.0中的响应主体

[英]GZIP the response body in PlayFramework 2.0

I am working on Playframework 2.x application. 我正在使用Playframework 2.x应用程序。 The controllers in my application return JSON response back to the browser/endpoint. 我的应用程序中的控制器将JSON响应返回给浏览器/端点。 I wanted to know if there is a simple way to enable GZIP compression of the response bodies. 我想知道是否有一种简单的方法来启用响应体的GZIP压缩。

Currently in play 2.0.4 there is no simple way for non assets. 目前在游戏2.0.4中,非资产没有简单的方法。

For the Java API you could use: 对于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;
}

gzip'ing is pretty much complete cake with an Apache front end. gzip'ing是Apache前端非常完整的蛋糕。

On Apache 2.4 gzip handling via Location block using a basic set of content types might look like: 在Apache 2.4 gzip上使用一组基本内容类型通过Location块处理可能如下所示:

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

In Play framework 2.2+ it is possible to use GzipFilter . Play framework 2.2+ ,可以使用GzipFilter Available via sbt: 可通过sbt获得:

libraryDependencies ++= filters

If you are a scala guy, it is worth looking at Gzip class . 如果你是一个scala家伙,那么值得一看Gzip课程

With Play 2.5, as mentioned here : 随着游戏2.5,提到这里

Here's a sample code to include gZip Filter(along with a sample CORS filter to showcase adding multiple filters): 这是一个包含gZip Filter的示例代码(以及一个示例CORS过滤器,用于展示添加多个过滤器):

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 };
    }

}

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

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