Spring MVC REST中的ETag处理

Jan*_*sel 11 java rest etag jax-rs spring-mvc

我正在考虑从使用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();
}
Run Code Online (Sandbox Code Playgroud)

我对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!
}
Run Code Online (Sandbox Code Playgroud)

即使不是必需的,我也必须预先为完整响应构建所有数据.这使得在Spring MVC REST中使用的ETag远远不如它有价值.根据我的理解,弱ETag的想法是,建立其价值并进行比较可能"便宜".在快乐的情况下,这可以防止服务器上的负载.在这种情况下,资源被修改,当然必须建立完整的响应.

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

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

  1. 有没有相当于 evaluatePreconditions()?
  2. 有没有更好的方法来构建ETag字符串?

谢谢你的想法!

ESa*_*ala 9

  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 ...;
}
Run Code Online (Sandbox Code Playgroud)

请注意,该checkNotModified方法有不同的版本,包括lastModified,ETag或两者.

您可以在此处找到文档:支持ETag和Last-Modified响应标头.


  1. 是的,但你必须先改变一些事情.

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

例如,如果此fetch-result是数据库查询,则可以向实体添加一个版本字段并对其进行注释,@Version以便每次修改它时都会增加,然后将该值用于ETag.

这取得了什么成果?因为您可以将提取配置为延迟,所以您不需要检索实体的所有字段,也可以避免使用哈希来构建ETag.只需将该版本用作ETag字符串即可.

这是关于在Spring Data项目中使用@Version的问题.