Why this matters
In Spring, failing to use `@PathVariable` with a path template placeholder (`/api/resource/{id}`) can cause unexpected behavior or missing parameter errors.
Ensure that `@PathVariable` is used correctly in Spring applications to bind URI path variables to method parameters.
In Spring, failing to use `@PathVariable` with a path template placeholder (`/api/resource/{id}`) can cause unexpected behavior or missing parameter errors.
Side-by-side examples engineers can pattern-match during review.
@GetMapping("/api/resource/{id}")
public ResponseEntity<String> getResourceById(Long id) { // Noncompliant - The 'id' parameter will not be automatically populated with the path variable value
return ResponseEntity.ok("Fetching resource with ID: " + id);
}@GetMapping("/api/resource/{id}")
public ResponseEntity<String> getResourceById(@PathVariable Long id) { // Compliant
return ResponseEntity.ok("Fetching resource with ID: " + id);
}@GetMapping("/api/resource/{id}")
public ResponseEntity<String> getResourceById(Long id) { // Noncompliant - The 'id' parameter will not be automatically populated with the path variable value
return ResponseEntity.ok("Fetching resource with ID: " + id);
}@GetMapping("/api/resource/{id}")
public ResponseEntity<String> getResourceById(@PathVariable Long id) { // Compliant
return ResponseEntity.ok("Fetching resource with ID: " + id);
}From the same buckets as this rule.