Spring 数据 REST - 如何按 JSON 被忽略的字段对实体进行排序?

Spring Data REST - How to sort an entity by JSON ignored field?

我正在使用 Spring Data REST,我的项目中有以下实体。

@Data
@Entity
public class Loan{

    @Id
    @GeneratedValue
    private Long id;

    @JsonIgnore
    private Long createdDate;

    private Long amount;

    private Long repaymentStartDate;

}

现在我想按 createdDate 排序贷款,它将自动填充和 JSONIgnored 以防止它被更新。但是当我调用端点 loans?sort=createdDate.

时,我无法按 createdDate 对贷款进行排序

我该如何解决这个问题?

这是我的存储库:

public interface LoanRepository extends PagingAndSortingRepository<Loan, Long>{

}

解决方法是尝试将 @JsonIgnore 替换为 @JsonProperty(access = READ_ONLY)。它可以防止 createdDate 发生变化,但会保留在 json 正文中。

已更新

对于 Spring Boot 1.5.10+ 而不是 @JsonProperty(access = READ_ONLY),您可以在实体之上使用 @JsonIgnoreProperties("createdDate")