Skip to lesson

Filtering, sorting and pagination with query parameters

A shop with 50,000 products cannot send them all to a phone. The app needs a way to ask for only some of them, in a set order, a few at a time.

Filters as query parameters

A filter is a query parameter that keeps only matching items: ?categoryId=4 keeps accessories. Several filters combine with AND.

Accessories that are also in stock.

Example 1 · Two filters together

Text
GET /api/v1/products?categoryId=4&inStock=true HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [
    { "id": 6, "sku": "ACC-AD141", "name": "boAt Airdopes 141", "categoryId": 4, "categoryName": "Accessories", "price": 1299.00, "stock": 120, "isActive": true },
    { "id": 7, "sku": "ACC-PB20K", "name": "Mi Power Bank 20000mAh", "categoryId": 4, "categoryName": "Accessories", "price": 2199.00, "stock": 60, "isActive": true }
  ],
  "page": 0,
  "size": 20,
  "totalElements": 2,
  "totalPages": 1
}

What this code does

  • Accessories (category 4) has four products, but the mouse has stock 0 and the USB-C cable is inactive, so two remain. Filters combine with AND.
  • The wrapper around the list is the page envelope, explained in its own section below. Without page and size, the defaults 0 and 20 apply.

Without filters, a client downloads everything and searches on its own: slow on a phone, wasteful for the server and impossible with 50,000 products. Every filter is optional; leaving it out means "no condition".

ShopLite's product list accepts q (name or SKU contains the text), categoryId, minPrice, maxPrice, inStock=true and includeInactive=true. A filter that matches nothing is a normal empty page (200), and a bad value such as minPrice=abc is a 400.

Sorting with sort=field,direction

An allow-list is a fixed list of the only values a server accepts, such as the fields a client may sort by. Anything else is refused with a 400.

Mobiles sorted by price, highest first, then a field that is not allowed.

Example 2 · Sorting, and a field that is not allowed

Text
GET /api/v1/products?categoryId=2&sort=price,desc HTTP/1.1Host: localhost:8080GET /api/v1/products?sort=password HTTP/1.1Host: localhost:8080
Responses
HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [
    { "id": 3, "sku": "MOB-IP15", "name": "iPhone 15 128GB", "categoryId": 2, "categoryName": "Mobiles", "price": 69900.00, "stock": 8, "isActive": true },
    { "id": 2, "sku": "MOB-GM35", "name": "Samsung Galaxy M35", "categoryId": 2, "categoryName": "Mobiles", "price": 19999.00, "stock": 25, "isActive": true },
    { "id": 1, "sku": "MOB-RN13", "name": "Redmi Note 13 5G", "categoryId": 2, "categoryName": "Mobiles", "price": 17999.00, "stock": 40, "isActive": true }
  ],
  "page": 0,
  "size": 20,
  "totalElements": 3,
  "totalPages": 1
}

HTTP/1.1 400 Bad Request
Content-Type: application/problem+json

{
  "type": "https://shoplite.example/problems/validation-failed",
  "title": "Validation failed",
  "status": 400,
  "detail": "The request has 1 invalid field.",
  "instance": "/api/v1/products",
  "errors": [
    { "field": "sort", "message": "must be one of id, name, price, stock" }
  ]
}

What this code does

  • The three mobiles come back from 69900.00 down to 17999.00. Without sort, ShopLite products are sorted by id ascending.
  • There is no password field, but the server does not even look: the field is not on the allow-list, so it answers 400 and names the allowed fields.

Sorting is one parameter in the form sort=field,direction: ?sort=price,desc gives the most expensive first, and asc (low to high) is the default direction. Every list also has a default sort, so the order never changes between two identical requests.

For ShopLite products the allow-list is id, name, price and stock. Without it, a client could sort by hidden columns and learn secrets from the order, and a careless server that pastes the text into SQL opens the door to SQL injection.

Ties are broken by id, so two products with the same price always come back in the same order and never jump between pages.

Page and size, with a maximum

Pagination splits a long list into pages. size is how many items one page holds and page is which page, counting from 0.

The five cheapest products, then a request with a page size that is too big.

Example 3 · First page of 5, cheapest first

Text
GET /api/v1/products?sort=price,asc&page=0&size=5 HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [
    { "id": 11, "sku": "BK-ATOMIC", "name": "Atomic Habits", "categoryId": 5, "categoryName": "Books", "price": 399.00, "stock": 50, "isActive": true },
    { "id": 9, "sku": "BK-CLEAN", "name": "Clean Code", "categoryId": 5, "categoryName": "Books", "price": 499.00, "stock": 30, "isActive": true },
    { "id": 10, "sku": "BK-PRAG", "name": "The Pragmatic Programmer", "categoryId": 5, "categoryName": "Books", "price": 650.00, "stock": 22, "isActive": true },
    { "id": 8, "sku": "ACC-M331", "name": "Logitech M331 Silent Mouse", "categoryId": 4, "categoryName": "Accessories", "price": 849.00, "stock": 0, "isActive": true },
    { "id": 12, "sku": "FSH-KURTA", "name": "Cotton Kurta", "categoryId": 6, "categoryName": "Fashion", "price": 899.00, "stock": 35, "isActive": true }
  ],
  "page": 0,
  "size": 5,
  "totalElements": 14,
  "totalPages": 3
}

What this code does

  • There are 14 active products, so 5 per page gives 3 pages. This is page 0: the five cheapest, from Atomic Habits (399.00) to the Cotton Kurta (899.00).
  • The inactive USB-C cable (199.00) does not appear: inactive products are hidden unless the request adds includeInactive=true.

Example 4 · Too big a page is refused

Text
GET /api/v1/products?page=-1&size=500 HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 400 Bad Request
Content-Type: application/problem+json

{
  "type": "https://shoplite.example/problems/validation-failed",
  "title": "Validation failed",
  "status": 400,
  "detail": "The request has 2 invalid fields.",
  "instance": "/api/v1/products",
  "errors": [
    { "field": "page", "message": "must be 0 or more" },
    { "field": "size", "message": "must be between 1 and 100" }
  ]
}

What this code does

  • Both parameters are wrong, and both are reported in one answer, so the client fixes them in one go.
  • The server does not quietly change 500 to 100. A silent change would make a client think it has all items when it only has the first 100.

?page=0&size=20 is items 1 to 20 and ?page=1&size=20 is items 21 to 40. Every list that can grow needs a maximum page size. Without one, a single request with size=1000000 makes the server load every row into memory, build a huge response and send it over a slow mobile network.

ShopLite uses a default size of 20 and a maximum of 100. A size above 100, or a negative page, is a 400 error and is not silently changed, so the client gets exactly the size it asked for or a clear reason why not.

The page envelope

A page envelope is a JSON object that wraps one page of results with the numbers a client needs to draw page buttons.

The last page of a list, and a page past the end.

Example 5 · The last page, and a page past the end

Text
GET /api/v1/products?sort=price,asc&page=2&size=5 HTTP/1.1Host: localhost:8080GET /api/v1/products?sort=price,asc&page=3&size=5 HTTP/1.1Host: localhost:8080
Responses
HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [
    { "id": 2, "sku": "MOB-GM35", "name": "Samsung Galaxy M35", "categoryId": 2, "categoryName": "Mobiles", "price": 19999.00, "stock": 25, "isActive": true },
    { "id": 4, "sku": "LAP-IPS3", "name": "Lenovo IdeaPad Slim 3", "categoryId": 3, "categoryName": "Laptops", "price": 42990.00, "stock": 12, "isActive": true },
    { "id": 5, "sku": "LAP-HPV15", "name": "HP Victus 15 Gaming", "categoryId": 3, "categoryName": "Laptops", "price": 64990.00, "stock": 5, "isActive": true },
    { "id": 3, "sku": "MOB-IP15", "name": "iPhone 15 128GB", "categoryId": 2, "categoryName": "Mobiles", "price": 69900.00, "stock": 8, "isActive": true }
  ],
  "page": 2,
  "size": 5,
  "totalElements": 14,
  "totalPages": 3
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [],
  "page": 3,
  "size": 5,
  "totalElements": 14,
  "totalPages": 3
}

What this code does

  • Page 2 holds items 11 to 14, only four: the last page can be short. totalPages is 14 ÷ 5 = 2.8, rounded up to 3.
  • Page 3 does not exist, but this is still 200 with an empty content: the totals tell the client it asked past the end.

ShopLite uses five fields: content (the items), page, size, totalElements (all matching items) and totalPages.

The last page may hold fewer items than size. A page past the end is not an error: it is 200 with an empty content and the real totals, so the client can see that it went too far. totalPages is totalElements ÷ size, rounded up.

Offset versus cursor pagination, and the Link header

Cursor pagination returns a cursor, an opaque token that marks the last item seen. The next request says ?after=<cursor>. Feeds and timelines use it.

A notification feed read with a cursor and a Link header.

Example 6 · Cursor pagination with a Link header (not used by ShopLite v1)

Text
GET /api/v1/notifications?size=2&after=bjo5MDQx HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 200 OK
Link: <http://localhost:8080/api/v1/notifications?size=2&after=bjo5MDM5>; rel="next"
Content-Type: application/json

{
  "content": [
    { "id": 9040, "text": "Order 15 was placed", "createdAt": "2026-09-27T09:55:00+05:30" },
    { "id": 9039, "text": "Order 14 was shipped", "createdAt": "2026-09-22T08:30:00+05:30" }
  ],
  "nextCursor": "bjo5MDM5"
}

What this code does

  • A notification feed changes all the time, so it uses a cursor. bjo5MDQx is an opaque token (here the last seen id, 9041, encoded); the client never builds or reads it.
  • The Link header gives the next URL with rel="next". There are no totals and no page numbers: cursors trade "jump to page 7" for stable, fast pages.

Page and size is offset pagination: the server skips page × size rows and returns the next size. It is simple and lets a user jump to page 7, but deep pages get slow because the database still walks past every skipped row, and a new row arriving between two requests shifts the items, so one can appear twice or be missed. A cursor avoids both: rows added before it shift nothing and every page is equally fast, but you cannot jump to page 7.

The Link header can carry the next and previous URLs, so clients just follow them. ShopLite v1 uses offset pagination, because people browse its lists page by page and the totals drive the page buttons; a cursor version could be added later without breaking v1.

Next step

Check your understanding

12 quick questions