Performance: Handling path variables and query params together
This affects server response time and client perceived load speed by how efficiently the backend handles URL parsing and routing.
@GetMapping("/items/{id}") public ResponseEntity<Item> getItem(@PathVariable String id, @RequestParam(required = false) String filter) { return ResponseEntity.ok(service.findItem(id, filter)); }
@GetMapping("/items/{id}") public ResponseEntity<Item> getItem(@PathVariable String id, HttpServletRequest request) { String filter = request.getParameter("filter"); // process with manual query param extraction return ResponseEntity.ok(service.findItem(id, filter)); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual query param extraction via HttpServletRequest | N/A (server-side) | N/A | N/A | [X] Bad |
| Using @PathVariable and @RequestParam annotations | N/A (server-side) | N/A | N/A | [OK] Good |
id and a query parameter sort in Spring Boot?id. Query parameters come after ?, so use @RequestParam for sort.id and @RequestParam for sort.@GetMapping("/items/{itemId}")
public String getItem(@PathVariable String itemId, @RequestParam(defaultValue = "asc") String order) {
return itemId + "-" + order;
}/items/42?order=desc?itemId is "42" from /items/42. Query parameter order is "desc" from ?order=desc.itemId + "-" + order, so "42-desc".@GetMapping("/books/{bookId}")
public String getBook(@PathVariable int bookId, @RequestParam int page) {
return "Book: " + bookId + ", Page: " + page;
}/books/abc?page=2?bookId as int, but URL provides "abc" which is not a number.filter. If filter is missing, it should default to "all". Which method signature correctly implements this behavior?userId.defaultValue = "all" to provide a default when filter is missing.Pass the test and this pattern moves unknown