有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringDataREST如何按JSON忽略字段对实体排序?

我正在使用SpringDataREST,我的项目中有以下实体

@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>{

}

共 (1) 个答案

  1. # 1 楼答案

    若要解决此问题,请尝试将@JsonIgnore替换为@JsonProperty(access = READ_ONLY)。它防止createdDate更改,但仍保留在json正文中

    已更新

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