对于版本化实体,在Spring中支持ETag

时间:2013-09-03 04:42:39

标签: java spring rest spring-mvc http-headers

我打算为我的RESTfull Spring应用程序支持ETag。我公开的大多数资源都是在DB中版本化的。

我知道ShallowEtagHeaderFilter,这不是我需要的,因为它只能节省带宽。

是否有针对Spring MVC的生产就绪解决方案,它将ETag标头与公开的实体版本相关联?

2 个答案:

答案 0 :(得分:7)

spring-data-rest支持这种开箱即用的功能,请参阅the conditional request part of the reference documentation

您还可以使用Spring Framework 4.2.0+,它支持Controller方法返回的ResponseEntity类型的条件请求 - see reference documentation

类似的东西:

@RequestMapping("/book/{id}")
public ResponseEntity<Book> showBook(@PathVariable Long id) {

    Book book = findBook(id);
    String version = book.getVersion();

    return ResponseEntity
                .ok()
                .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
                .eTag(version) // lastModified is also available
                .body(book);
}

答案 1 :(得分:0)

您可以使用ServletWebRequest.checkNotModified来检查自定义eTag,在您的情况下,它将是版本号。同样,只有当您的内容更改保证版本号发生更改时,这才有效。

更多详情here

相关问题