2

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

}
3
  • 1
    How does the restcontroller looks like? Please add the code to you question. Commented Feb 18, 2018 at 16:42
  • Since I am using Spring Data REST there is no need to write separate controller. Spring will generate it when I extend the repository interface. Commented Feb 18, 2018 at 16:48
  • When the field is annotated with ‘@JsonIgnore’ the field is not visible for the user. So it is clear that it is not possible to sort the result by something the user cannot see. Commented Feb 18, 2018 at 17:02

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.