San*_*hle 2 etag http-headers playframework-2.0
我已经搜索过在 playframework 中设置 Etag 。我得到的只是
1) https://www.playframework.com/documentation/2.0/Assets
2) https://www.playframework.com/documentation/2.0/JavaResponse
第一个选项仅适用于资产或文件。第二个选项不起作用。我刚刚添加了示例中给出的三个。
实际上 Etag 的用法在 Play 的资产文档(您首先链接的)以及维基百科典型用法部分中有所描述。
动作开始时需要判断请求的资源自上一代Etag后是否有变化,如果是,则需要用new Etag生成新内容,否则只返回304 NotModified响应。
当然,这一切都取决于所请求资源的类型,无论如何,相当干净的样本可能是一个数据库实体,带有一些 ID 和带有上次修改日期/时间的字段:
public static Result eTaggedFoo(Long id) {
Foo foo = Foo.find.byId(id);
if (foo == null) return notFound("Given Foo was not found");
String eTag = DigestUtils.md5Hex(foo.id + foo.lastModification.toString());
String ifNoneMatch = request().getHeader("If-None-Match");
if (ifNoneMatch != null && ifNoneMatch.equals(eTag)) return status(304);
response().setHeader(ETAG, eTag);
return ok("Here you can see the " + foo.name + ", last modified: " + foo.lastModification.toString());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2181 次 |
| 最近记录: |