说到HTTP条件请求,简单来说,得益于Etag和If-*,我们可以实现响应缓存(Etag+If-None-Match)和乐观锁(Etag+If-Match)。
如我所见,使用提供特定文件管理器 的 Spring 执行响应缓存很方便。 ShallowEtagHeaderFilter 生成 Etag 值并根据 If-None-Match header 检查它。但是,我在 Spring 中找不到相应的组件来做乐观锁。因此,我如何在 Spring 中实现它?
最佳答案
我遇到过同样的问题。在 Spring 中没有找到任何本地方式来检查它。但是您始终可以提取标题并进行手动比较。例如(在 Kotlin 中):
@PutMapping
fun update(
@RequestHeader("If-Match") ifMatch: String?
) : ResponseEntity<Void>{
if(ifMatch != null && ifMatch.trim() != computeETag()){
return ResponseEntity.status(412).build()
}
关于Spring 对 If-Match Header 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278484/