Bird
0
0

Which of the following is the correct syntax to use @PathVariable in a Spring Boot controller method to get an id from the URL /user/{id}?

easy📝 Syntax Q12 of 15
Spring Boot - REST Controllers
Which of the following is the correct syntax to use @PathVariable in a Spring Boot controller method to get an id from the URL /user/{id}?
A@GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... }
B@GetMapping("/user") public String getUser(@PathVariable int id) { ... }
C@GetMapping("/user/{id}") public String getUser(@RequestParam int id) { ... }
D@GetMapping("/user/{id}") public String getUser(@PathVariable String name) { ... }
Step-by-Step Solution
Solution:
  1. Step 1: Match URL pattern with method parameter

    The URL has {id}, so the method must have @PathVariable int id to match the placeholder.
  2. Step 2: Check annotation and parameter type

    @GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... } correctly uses @PathVariable with int id and matches the URL pattern.
  3. Final Answer:

    @GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... } -> Option A
  4. Quick Check:

    URL {id} matches @PathVariable int id [OK]
Quick Trick: URL placeholder and @PathVariable name must match [OK]
Common Mistakes:
  • Missing {id} in URL path
  • Using @RequestParam instead of @PathVariable
  • Mismatching parameter name and URL placeholder

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes