Spring Boot REST Design for Search API

Sujit picture Sujit · Feb 20, 2018 · Viewed 7.6k times · Source

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
...
}

Answer

de-jcup picture de-jcup · Feb 20, 2018

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