我正在尝试使用 Jersey 开发 RESTful API。我有 GET API 用于特定的获取操作,我的 GET 从同一客户端获取相同的时间。 是否可以缓存响应?感谢任何指点。
谢谢
最佳答案
您可以使用 CacheControl、eTag - 按照下面的示例代码
// In your jersey method
final EntityTag eTag = new EntityTag(resource.getId() + "_" +
resource.getLastModified().getTime());
final CacheControl cacheControl = new CacheControl();
cacheControl.setMaxAge(-1);
ResponseBuilder builder = request.evaluatePreconditions(
resource.getLastModified(), eTag);
// the resoruce's information was modified, return it
if (builder == null) {
builder = Response.ok(resource);
}
// the resource's information was not modified, return a 304
return builder.cacheControl(cacheControl).lastModified(
resource.getLastModified()).tag(eTag).build();
将 resource
替换为您的 Resource 实例。
关于java - 如何使用 Jersey 缓存响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15087831/