Search code examples
spring-bootapi-designspring-rest

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

Solution

  • 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