Skip to content

How to properly filter Spring REST data

I have a Spring Data REST service with a single @Entity and Repository. When I run

$ curl localhost:8080/api

I get all the data stored in my repository and it works as expected. I also have a small React front end and I display that data there.

My question is: Where should I filter the data? For example maybe I want all the entries with id > 10. Should I just filter the response in my front end or should I make the REST call in such a way that it returns just the required entries?

If I should do the latter, then how?




If it is always the case, why would you put extra burden on front-end shoulders to filter the results all the time?

Implement a new method which returns the desired results(e.g id > 10) and annotate it with @Query and provide JPQL or native query inside it

@Query("SELECT c FROM Customer c WHERE > 10")
Collection<Customer> findAllActiveCustomers();

However, if you choose native query do not forget to put nativeQuery = true inside @Query

User contributions licensed under: CC BY-SA
6 People found this is helpful