Do You Manually Write Filtering/Sorting for Each API or Use a Generic Solution?
In most applications (99.9% of them), we typically have a listing API for resources that supports filtering, sorting, and searching.
From what I’ve seen, developers usually write custom query logic or conditions for each of these operations manually, per resource.
However, I find this to be repetitive and somewhat labor-intensive across multiple projects.
So my question is: Should developers manually create specifications (or query conditions) for each resource's search, filter, and sort functionality? Or would it be better to automate this with a generic wrapper or utility that reduces the need to write such logic repeatedly?
What’s the best practice in modern API development for handling this scenario efficiently?
16 Replies
⌛ This post has been reserved for your question.
Hey @MrSharp! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
most of the time i saw people writing the filtration manually
there are some libraries for this tho
well yeah thats why i am confused, if there is already libraries like spring-filters why do people still write manual stuff?
i guess there might be some project specific filters you wanna add
that a framework doesnt really support
so then its just easier to create a custom filter
because at some extend and buisness logic, you cant have a general solution. ive worked at a company, where sql queries habe 200 line of code. thats impossible to automate lol is zhat what you mean?
GitHub
GitHub - tkaczmarzyk/specification-arg-resolver: An alternative API...
An alternative API for filtering data with Spring MVC & Spring Data JPA - tkaczmarzyk/specification-arg-resolver
@Suika i am talking about this
see
this implements QueryDSL and transform into JPA Predicates
so easy way to implement filtering and searching
along with complex operations like
gt, le
etc
i doubt you csn map a 200 line swl query which is nested 3 times with this.
for simple queries sure
it can for sure
nesting query with n level of depth
i have already implement an alternative to this in nodejs
the codebase is huge, 150+ endpoints and not 200 but even for 200+ sql query scenario, it works fine.
it have been 1.5years and i didnt found any issue
i was looking for similar approach in java
then its way easier to just use @Query(<statment>) of imo instead of understanding this lib. if you need to refactor tue statement it needs to be updated on one point. this looks like you need to change 4 classes
how do you test this?
how can i test my 200 lines of code db query with this
directly raw query i wont be able to write conditional queries or i mean its possible but it would become soo mess that any it will take time for a new guy just to understand it
e2e API test case?
normal @DataJdbcTest of what its called
if e2e covers that why need of unit testing?
its not a unit test
i mean still if e2e covers it
why need that?
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.