gpt4 book ai didi

java - Spring MVC REST 中的 ETag 处理

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:48 26 4
gpt4 key购买 nike

我正在考虑从使用 JAX RS 的 Apache CXF RS 切换到 Spring MVC REST,发现 Spring MVC REST 当前处理 ETag 的方式存在一些问题。也许我理解不正确,或者有更好的方法来实现目前使用 JAX RS 所做的事情?

使用 Apache CXF RS,在 REST 服务内部评估最后修改时间戳和 ETag 的条件(条件评估实际上非常复杂,请参阅 RFC 2616 第 14.24 和 14.26 节,所以我很高兴这是为我完成的) .代码看起来像这样:

@GET
@Path("...")
@Produces(MediaType.APPLICATION_JSON)
public Response findBy...(..., @Context Request request) {
... result = ...fetch-result-or-parts-of-it...;
final EntityTag eTag = new EntityTag(computeETagValue(result), true);
ResponseBuilder builder = request.evaluatePreconditions(lastModified, eTag);
if (builder == null) {
// a new response is required, because the ETag or time stamp do not match
// ...potentially fetch full result object now, then:
builder = Response.ok(result);
} else {
// a new response is not needed, send "not modified" status without a body
}
final CacheControl cacheControl = new CacheControl();
cacheControl.setPrivate(true); // store in private browser cache of user only
cacheControl.setMaxAge(15); // may stay unchecked in private browser cache for 15s, afterwards revalidation is required
cacheControl.setNoTransform(true); // proxies must not transform the response
return builder
.cacheControl(cacheControl)
.lastModified(lastModified)
.tag(eTag)
.build();
}

我尝试用 Spring MVC REST 做同样的事情看起来像这样:

@RequestMapping(value="...", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<...> findByGpnPrefixCacheable(...) {
... result = ...fetch-result...;
// ... result = ...fetch-result-or-parts-of-it...; - can't fetch parts, must obtain full result, see below
final String eTag = "W/\""+computeETagValue(result)+"\""; // need to manually construct, as opposed to convenient JAX RS above
return ResponseEntity
.ok() // always say 'ok' (200)?
.cacheControl(
CacheControl
.cachePrivate()
.maxAge(15, TimeUnit.SECONDS)
.noTransform()
)
.eTag(eTag)
.body(result); // ETag comparison takes place outside controller(?), but data for a full response has already been built - that is wasteful!
}

我对必须事先构建完整响应的所有数据持异议,即使这不是必需的。这使得在 Spring MVC REST 中使用的 ETag 的值(value)远不如它应有的值(value)。根据我的理解,弱 ETag 的想法是建立其值(value)并进行比较可能“便宜”。在令人满意的情况下,这可以防止服务器上的负载。在这种情况下,资源被修改了,当然必须构建完整的响应。

在我看来,Spring MVC REST 目前的设计要求构建完整的响应数据,无论最终是否需要响应主体。

因此,总而言之,Spring MVC REST 有两个问题:

  1. 是否有等效于 evaluatePreconditions() 的函数?
  2. 是否有更好的 ETag 字符串构造方法?

谢谢你的想法!

最佳答案

  1. ,有一个等效的方法。

你可以这样使用它:

@RequestMapping
public ResponseEntity<...> findByGpnPrefixCacheable(WebRequest request) {

// 1. application-specific calculations with full/partial data
long lastModified = ...;
String etag = ...;

if (request.checkNotModified(lastModified, etag)) {
// 2. shortcut exit - no further processing necessary
// it will also convert the response to an 304 Not Modified
// with an empty body
return null;
}

// 3. or otherwise further request processing, actually preparing content
return ...;
}

请注意,checkNotModified 方法有不同的版本,有 lastModified、ETag 或两者都有。

您可以在此处找到文档:Support for ETag and Last-Modified response headers .


  1. ,但你必须先改变一些东西。

您可以更改计算 ETag 的方式,这样您就不需要获取完整的结果。

例如,如果这个 fetch-result 是一个数据库查询,你可以在你的实体中添加一个版本字段并用 @Version 注释它,这样它每次被修改时都会递增,并且然后将该值用于 ETag。

这有什么用?由于您可以将获取配置为惰性的,因此您不需要检索实体的所有字段,并且还避免了必须对其进行散列以构建 ETag。只需使用版本作为 ETag 字符串即可。

这是关于 Using @Version in Spring Data project 的问题.

关于java - Spring MVC REST 中的 ETag 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com