Bird
0
0

Identify the error in the following Spring Boot controller method:

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
Identify the error in the following Spring Boot controller method:
@GetMapping("/user/{id}")
public String getUser(@PathVariable int userId) {
    return "User ID: " + userId;
}
AThe @PathVariable parameter name does not match the path variable name
BThe method should return a ResponseEntity instead of String
CThe @GetMapping path should not contain curly braces
DThe method parameter should be of type String, not int
Step-by-Step Solution
Solution:
  1. Step 1: Compare path variable name and method parameter

    The path is defined as /user/{id} but the method parameter is named userId without specifying the variable name in @PathVariable.
  2. Step 2: Understand @PathVariable name matching

    By default, the parameter name must match the path variable name or be explicitly specified like @PathVariable("id") int userId.
  3. Final Answer:

    The @PathVariable parameter name does not match the path variable name -> Option A
  4. Quick Check:

    Path variable names must match or be explicitly linked [OK]
Quick Trick: Match @PathVariable name with URL placeholder or specify explicitly [OK]
Common Mistakes:
  • Not matching method parameter name with path variable
  • Assuming type mismatch causes error here
  • Thinking curly braces are invalid in @GetMapping path

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes