When to use @QueryParam vs @PathParam
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Use @PathParam when the parameter identifies a specific resource. Use @QueryParam when the parameter filters, sorts, or modifies the response for a collection. The rule is straightforward: if removing the parameter would change which resource you are talking about, it belongs in the path. If removing it would return the same resource with less refinement, it belongs in the query string.
Path Parameters: Resource Identity
Path parameters are part of the URL path itself. They define the hierarchical structure of your API and identify specific resources or sub-resources. In REST, every resource has a unique URI, and path parameters are what make that URI unique.
The URL /organizations/5/teams/12/members/42 unambiguously identifies one member. Remove any segment and you are talking about a different resource (the team, the organization, or the collection of members).
Characteristics of Path Parameters
- They are required. Omitting a path segment results in a different URL that routes to a different endpoint or a 404.
- They represent hierarchy. Each segment narrows the scope from broad (organization) to specific (member).
- They are positional. The order matters and is defined by the URL template.
- They contribute to cacheability. Proxies and CDNs cache responses by URL, and path-based URLs produce clean cache keys.
Query Parameters: Filtering and Modification
Query parameters appear after the ? in the URL. They modify how the server processes the request without changing which resource is being addressed.
The URL /products?category=electronics&sort=price&page=2 still addresses the products collection. The query parameters narrow and shape the result set.
Characteristics of Query Parameters
- They are optional. Omitting them returns a default or unfiltered response.
- They support default values via
@DefaultValue. - They can accept multiple values for the same key (
?tag=java&tag=spring). - They are not part of the resource identity. Two URLs that differ only in query parameters address the same resource.
Decision Framework
Use this decision tree when designing an endpoint:
| Question | Path if Yes | Query if Yes |
| Does the parameter identify a resource? | Yes | |
| Is the parameter required for routing? | Yes | |
| Does removing it change the resource? | Yes | |
| Is the parameter optional? | Yes | |
| Does it filter a collection? | Yes | |
| Does it paginate or sort results? | Yes | |
| Does it have a sensible default? | Yes |
Real-World URL Design
Combining Path and Query Parameters
Most production APIs use both. Path parameters establish which resource or collection you are addressing, and query parameters refine the response:
Spring MVC Equivalents
In Spring MVC, the same concepts apply with different annotations:
| JAX-RS | Spring MVC | Purpose |
@PathParam | @PathVariable | Extract from URL path |
@QueryParam | @RequestParam | Extract from query string |
@DefaultValue | defaultValue attribute | Set fallback value |
HTTP Method Interactions
The choice between path and query parameters also depends on the HTTP method:
For POST and PUT, the request body carries the payload. Query parameters on write operations are uncommon and usually limited to control flags like ?dryRun=true.
Common Pitfalls
Using query parameters for resource identification. Designing /users?id=42 instead of /users/42 breaks REST conventions. The resource should be identifiable by its URL path alone. Query parameters on a GET that returns a single resource is a code smell.
Putting optional filters in the path. A URL like /users/admin/active/page/3 encodes filters as path segments, making the URL rigid and hard to extend. Adding a new filter requires a new path template. Query parameters (/users?role=admin&status=active&page=3) are more flexible and can be added without changing the routing.
Forgetting @DefaultValue on query parameters. Without a default, omitting a query parameter passes null (for objects) or zero (for primitives) into your method. Always specify @DefaultValue for optional parameters to avoid null-handling bugs.
Encoding issues in path parameters. Path parameters containing slashes, dots, or special characters can conflict with URL routing. A username like john.doe may be interpreted as a file extension. Use regex constraints in your path template ({username: [a-zA-Z0-9_]+}) or encode the value.
Over-nesting path parameters. URLs like /a/{aId}/b/{bId}/c/{cId}/d/{dId} become unwieldy. If the nesting goes beyond three levels, consider whether the deep resource can be addressed directly (/d/{dId}) with the parent relationship expressed in the response body instead.
Using @QueryParam for sensitive data. Query parameters appear in server logs, browser history, and referrer headers. Never pass passwords, tokens, or personally identifiable information as query parameters. Use headers or the request body instead.
Summary
@PathParamidentifies a resource and is part of the URL structure. It is required and positional.@QueryParamfilters, sorts, or paginates a response. It is optional and supports default values.- Use path parameters for hierarchical resource identification (
/users/{id}/orders/{orderId}). - Use query parameters for collection modifiers (
?status=active&sort=date&page=2). - In Spring MVC, the equivalents are
@PathVariableand@RequestParam. - Avoid putting optional filters in the path, and never pass sensitive data in query strings.
Related reading
- When to use tensorflow datasets api versus pandas or numpy
- When to using async when dealing with TcpClients?
- Where can I find a NuGet package for upgrading to System.Web.Http v5.0.0.0?
- Where can I get a list of Kubernetes API resources and subresources?
- Where is HttpContent.ReadAsAsync?
- Which API Group in k8s
- Which DHT algorithm to use (if I want to join two separate DHTs)?
- Which metrics should I use for an alarm HTTPCode_Target_5XX_Count or HTTPCode_ELB_5XX_Count?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.