Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot REST Design for Search API

I am designing a SpringBoot RESTful API for a Product searching with various attributes (search can be one or more). Few of the criteria are greater than a certain amount and few are less than. In the @RequestParam we can take String or similar values but not any criteria.

My question is what's the best way to get the user data for these criteria in a GET search API call

@GetMapping("/search")
public ResponseEntity<List<OrderView>> searchOrders(...)
{
...
// call to service implementation
...
}
like image 229
Sujit Avatar asked Jan 03 '23 18:01

Sujit


2 Answers

Hmm... https://spring.io/guides/tutorials/bookmarks/ has a good description about REST services with spring. It has also description about the different levels when it comes to RESTful principles (and you can do HATEOAS very simple and clever with spring-boot by HAL).

When you are doing a search you do not have the resource (level1) url but you want to obtain it... So it's okay (imho) to do a simple query parameter call. For example when looking at amazon.com and typing some search parameters there, you will see that they are using a simple approach:

https://www.amazon.de/s/ref=nb_sb_noss?....&url=search-alias%3Daps&field-keywords=criteria1+criteria2+criteria3

They just add the keywords as a concatenated string.

There is also a interesting blog entry from apigee available: https://apigee.com/about/blog/technology/restful-api-design-tips-search

like image 149
de-jcup Avatar answered Feb 19 '23 05:02

de-jcup


In this article Spring Boot: How to design efficient REST API?, I explained how to develop a REST API for search. As an example, you found the code and screenshots of postman in this article.

As optimization with one endpoint, I can get several results: resources are sorted, filtered, and paginated.

You do not deal with a lot of code (check of query param and all the control in your controller): the library specification-arg-resolver makes that with no problem

like image 24
Raouf Makhlouf Avatar answered Feb 19 '23 06:02

Raouf Makhlouf