Spring Pathvariable Mismatch

ID

java.spring_pathvariable_mismatch

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, spring

Description

Reports Spring MVC handler methods where the path template variables (e.g. {id}) do not match the @PathVariable method parameters, or vice versa. A mismatch causes a runtime MissingPathVariableException.

Rationale

Reliability — The handler method will fail at runtime when the missing path variable is resolved, returning a 500 error to the client.

Remediation

Non-compliant code

@GetMapping("/users/{id}")
public String getUser(@PathVariable String userId) {
    return userId;
}

Compliant code

@GetMapping("/users/{id}")
public String getUser(@PathVariable("id") String userId) {
    return userId;
}