Performance: @PathVariable for URL parameters
This affects server response time and client perceived load speed by how URL parameters are parsed and routed.
@GetMapping("/user/{id}") public ResponseEntity<String> getUser(@PathVariable("id") String id) { // process id directly return ResponseEntity.ok("User " + id); }
public ResponseEntity<String> getUser(HttpServletRequest request) {
String id = request.getParameter("id");
// process id
return ResponseEntity.ok("User " + id);
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual parameter parsing with HttpServletRequest | N/A (server-side) | N/A | N/A | [X] Bad |
| Using @PathVariable annotation | N/A (server-side) | N/A | N/A | [OK] Good |
@PathVariable annotation in Spring Boot?@PathVariable in a Spring Boot controller method to get an id from the URL /user/{id}?@GetMapping("/product/{category}/{id}")
public String getProduct(@PathVariable String category, @PathVariable int id) {
return category + "-" + id;
}/product/electronics/42?@GetMapping("/order/{orderId}")
public String getOrder(@PathVariable int id) {
return "Order: " + id;
}/order/100?/blog/{year}/{month}/{slug} where year and month are integers and slug is a string. Which of the following method signatures correctly uses @PathVariable to extract these values?Pass the test and this pattern moves unknown