简体   繁体   中英

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

I am using Spring Data REST and I have the following entity in my project.

@Data
@Entity
public class Loan{

    @Id
    @GeneratedValue
    private Long id;

    @JsonIgnore
    private Long createdDate;

    private Long amount;

    private Long repaymentStartDate;

}

Now I want to sort the loans by the createdDate which will be automatically filled and JSONIgnored to prevent it from being updated. But I am unable to sort the loans by the createdDate when I call the endpoint loans?sort=createdDate .

How do I fix this?

Here is my repository:

public interface LoanRepository extends PagingAndSortingRepository<Loan, Long>{

}

To workaround try to replace @JsonIgnore to @JsonProperty(access = READ_ONLY) . It prevents createdDate from changing but remains it in the json body.

UPDATED

For Spring Boot 1.5.10+ instead of @JsonProperty(access = READ_ONLY) you can use @JsonIgnoreProperties("createdDate") on top of the entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM