Allowing filtering, sorting, and pagination

Since REST is basically a style and not a specification for services, there are aspects that aren't specified, and you have some leeway as to their implementation. Three common requirements are filtering (so you don't get all entities, but just those that satisfy some condition), sorting (so that entities are included in some order), and pagination (because showing hundreds or thousands of entities at once isn't practical). Of course, these three requirements interact with each other; if you sort and filter, then paging should apply to the sorted filtered data.

All of these requirements can be handled by adding some query parameters (or possibly headers), but you'll have to study a bit to understand what's the best way for you:

  • Filtering may be specified with a format such as filter=price[lt]220, which would specify that a given attribute (price) must be less than (lt) a value (200). Building up more complex expressions involving logical operators such as and, or, and not, plus optional parentheses, can also be done, at the cost of more complex parsing and interpreting at the server.
  • Sorting may be specified by parameters such as sortby=price,name to order first by price and then by name. You can add other options to allow for ascending or descending sorting.
  • Paging can be done by using the limit and offset parameters, with the same interpretation that's used in SQL SELECT statements (see https://dev.mysql.com/doc/refman/8.0/en/select.html for more on that) or by specifying a page size and the page number.

Adding the handling of these options to your REST server will make it more powerful, and enable the client to send more specific, optimized requests. There is one more extension that you may want; being able to select extra, related entities, so read on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.156