REST API DESIGN - Getting a resource through REST with different parameters but same url pattern
Categories:
REST API Design: Handling Multiple Parameters with a Single URL Pattern

Explore best practices for designing RESTful APIs that retrieve resources using various parameters while maintaining a consistent URL structure. Learn how to manage optional and mandatory parameters effectively.
Designing robust and intuitive REST APIs often involves retrieving resources based on various criteria. A common challenge arises when you need to fetch the same logical resource but with different filtering, sorting, or pagination parameters. This article delves into how to achieve this while adhering to REST principles, ensuring a clean and consistent URL pattern.
The Challenge: Different Parameters, Same Resource
Consider an API endpoint for retrieving a list of products. You might want to get all products, products by category, products by price range, or even a combination of these. If each combination required a distinct URL path, your API would quickly become unwieldy and difficult to maintain. The goal is to use a single, clear URL pattern for the resource and differentiate requests using query parameters.
flowchart TD A[Client Request] --> B{GET /products} B --> C{Check Query Parameters?} C -->|No Parameters| D[Return All Products] C -->|category=Electronics| E[Filter by Category] C -->|minPrice=100&maxPrice=500| F[Filter by Price Range] C -->|category=Books&sortBy=title| G[Filter & Sort] E --> H[Return Filtered Products] F --> H G --> H
Flowchart illustrating how a single /products endpoint handles various query parameters.
Leveraging Query Parameters for Flexibility
The standard and most RESTful approach for handling different parameters for the same resource is to use query parameters. Query parameters are appended to the URL after a question mark (?) and are separated by ampersands (&). They do not alter the resource's identity but rather modify the representation or subset of the resource being returned.
GET /products
GET /products?category=electronics
GET /products?minPrice=100&maxPrice=500
GET /products?category=books&sortBy=title&order=asc
GET /products?page=2&pageSize=10
Examples of using query parameters for filtering, sorting, and pagination.
sortBy
instead of sort
and pageSize
instead of limit
.Implementation Considerations (JAX-RS Example)
In frameworks like JAX-RS (Java API for RESTful Web Services), handling query parameters is straightforward. You can inject them directly into your resource methods using the @QueryParam
annotation. The framework handles the parsing and mapping for you.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/products")
public class ProductResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getProducts(
@QueryParam("category") String category,
@QueryParam("minPrice") Double minPrice,
@QueryParam("maxPrice") Double maxPrice,
@QueryParam("sortBy") String sortBy,
@QueryParam("order") @DefaultValue("asc") String order,
@QueryParam("page") @DefaultValue("1") int page,
@QueryParam("pageSize") @DefaultValue("20") int pageSize) {
// Logic to fetch products based on provided parameters
// If a parameter is null, it means it wasn't provided in the URL
// Use default values for pagination and sorting order if not specified
System.out.println("Category: " + category);
System.out.println("Min Price: " + minPrice);
System.out.println("Max Price: " + maxPrice);
System.out.println("Sort By: " + sortBy);
System.out.println("Order: " + order);
System.out.println("Page: " + page);
System.out.println("Page Size: " + pageSize);
// Example: return a dummy list of products
String result = "{\"message\": \"Fetching products with parameters...\"}";
return Response.ok(result).build();
}
}
JAX-RS example demonstrating how to handle multiple optional query parameters for a single GET endpoint.
@DefaultValue
annotation in JAX-RS is extremely useful for providing fallback values for optional query parameters, simplifying your application logic.