我们通过 Spring Boot (v1.3.3) 使用 Spring Data Rest,并通过标准 REST 存储库导出器公开以下实体:
@Document
public class Project {
@Version
private Long version;
@CreatedDate
private LocalDateTime createdDate;
@CreatedBy
private String createdBy;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@LastModifiedBy
private String lastModifiedBy;
private String name;
//getters and setters omitted
}
例如,对/projects 的 POST 或对/projects/1234 的 PUT 会创建一个 etag/版本 0 的文档。即使客户端不提供版本/etag,对/projects/1234 的另一个 PUT 也会覆盖该文档。此外,即使客户端不提供版本/etag,/projects/1234 上的 DELETE 也会删除该文档。
当且仅当客户端提供有效的版本/etag时,如何配置 Spring Data Rest 来更新和删除文档?
最佳答案
API 客户端需要在请求的 header 上指定有效的 ETag,如下所示:
If-Match: <value of previous ETag>
通过将此 header 添加到 HTTP 请求中,您可以将其转换为条件操作,只有在满足条件时才会执行该操作。还有其他条件,例如 If-None-Match
或If-Modified-Since
.
客户端有责任添加条件 header 。
从这个问题中我了解到您希望服务器禁止任何不提供有效ETag的操作,但据我所知Spring Data Rest不支持这一点。
您可以更改您的客户端以包含正确的 header (假设它们在您的控制之下),或者您自己实现此功能。
您可以查看Reference Documentation欲了解更多信息。
关于java - 如何强制客户端使用 Spring Data Rest 发送 etag/版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36575478/