我有一个问题是为缓存的响应发送标头。
这是我的路线,
GET /fassets/XXX.js com.AssetController.getFileXXX()
我的控制器,
@Cached(key = XXX, duration = TIMEOUT_SECONDS,)
public Result getFileXXX() {
[...]
final Result result = ok(file).as("application/javascript");
response().setHeader(CACHE_CONTROL, "max-age=100");
return result;
}
问题在于,对于第一个请求,正确发送了cache-control:max-age = 100。 对于其他人,结果是缓存的,并且未设置标题。
我试过
result.headers().put(CACHE_CONTROL, "max-age=10")
但这会导致运行时错误:
Caused by: java.lang.UnsupportedOperationException: null
at java.util.AbstractMap.put(AbstractMap.java:209) ~[na:1.8.0_45]
我还发现了一些可以解决问题的有趣内容,但是在scala中:
val result = Ok("Hello World!").withHeaders(CACHE_CONTROL -> "max-age=3600")
感谢您的帮助。
于连
答案 0 :(得分:0)
我认为answer by Codemwnci也适用于您的问题:
您需要查看JavaResponse section。
从文档中设置缓存控制的示例是
public static Result index() {
response().setContentType("text/html");
response().setHeader(CACHE_CONTROL, "max-age=3600");
response().setHeader(ETAG, "xxx");
return ok("<h1>Hello World!</h1>");
}
我会在调用response().setHeader
之前设置ok
内容。