【问题标题】:GZIP the response body in PlayFramework 2.0GZIP PlayFramework 2.0 中的响应正文
【发布时间】:2012-10-28 05:03:14
【问题描述】:

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

【问题讨论】:

  • 如果 Play 存在于前端(apache、nginx 等)之后,那么就在前端这样做,简单、直接、有效
  • 如果您将此作为答案,我会接受:)
  • 当然,我会包括我的方法,秒

标签: playframework playframework-2.0 gzip


【解决方案1】:

目前在 play 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;
}

【讨论】:

    【解决方案2】:

    gzip'ing 是一个非常完整的带有 Apache 前端的蛋糕。

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

    【讨论】:

      【解决方案3】:

      Play framework 2.2+ 中可以使用GzipFilter。可通过 sbt 获得:

      libraryDependencies ++= filters
      

      如果你是 scala 人,值得一看 Gzip class

      【讨论】:

        【解决方案4】:

        使用 Play 2.5,如 here 所述:

        这是包含 gZip 过滤器的示例代码(以及用于展示添加多个过滤器的示例 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 };
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 2019-02-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多