如何 select 在 Spring-data-rest 的 PATCH 操作中将更新哪些属性?

How to select what properties are going to be updated in the PATCH operation from Spring-data-rest?

假设我有这个人 class:

class Person{ 
  private String id; 
  private String name; 
  private Date creationDate;
}

使用 spring 数据剩余我可以像这样修补这个实体:

http://resourcehost/people/personId

json请愿正文:

{
  "name":"Jon Smith",
  "creationDate":"2020-08-01 00:00:00"
}

我希望我的客户能够更新名称,但我不希望消费者更新 "creationDate" 字段。

如何配置是否应使用 PATCH 操作更新属性?

我正在使用 Spring boot statrters 版本 2.2。2.RELEASE

您可以通过使用 @JsonProperty(access = JsonProperty.Access.READ_ONLY) 注释来忽略 PATCH 请求中实体特定字段的更新,正如下面 class 中所做的那样。

@MappedSuperclass
public abstract class ModelBase implements Serializable {
    @Column(name = "creation_time")
    @CreatedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime creationTime;

    @Column(name = "modified_time")
    @LastModifiedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private LocalDateTime modifiedTime;
}

Spring Data REST 不会抛出异常,它会简单地忽略该字段。通过使用 Projections 你应该能够忽略这种行为。