Bird
0
0

Which of the following method signatures correctly uses @GetMapping and @PathVariable annotations?

hard📝 state output Q15 of 15
Spring Boot - REST Controllers
You want to create a Spring Boot controller method that handles GET requests to /product/{category}/{id} and returns a string combining both path variables. Which of the following method signatures correctly uses @GetMapping and @PathVariable annotations?
A@GetMapping("/product/{category}/{id}") public String getProduct(String category, int id) { return category + "-" + id; }
B@GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable String cat, @PathVariable int id) { return cat + "-" + id; }
C@GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable int category, @PathVariable String id) { return category + "-" + id; }
D@GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; }
Step-by-Step Solution
Solution:
  1. Step 1: Check @GetMapping path and method parameters

    The path has two variables: {category} and {id}. The method must accept two parameters annotated with @PathVariable matching these names or explicitly linked.
  2. Step 2: Analyze each option's parameter annotations and types

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable String cat, @PathVariable int id) { return cat + "-" + id; } uses parameter names that do not match path variables (cat != category), so fails without explicit names. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } explicitly links parameter names to path variables, which is safest. @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable int category, @PathVariable String id) { return category + "-" + id; } swaps types incorrectly. @GetMapping("/product/{category}/{id}") public String getProduct(String category, int id) { return category + "-" + id; } lacks @PathVariable annotations, so variables won't be bound.
  3. Step 3: Choose the best correct and safe option

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } explicitly names variables, avoiding errors if parameter names differ from path variables.
  4. Final Answer:

    @GetMapping("/product/{category}/{id}") public String getProduct(@PathVariable("category") String cat, @PathVariable("id") int id) { return cat + "-" + id; } -> Option D
  5. Quick Check:

    Explicit @PathVariable names ensure correct binding [OK]
Quick Trick: Use @PathVariable("name") if parameter name differs from path variable [OK]
Common Mistakes:
  • Omitting @PathVariable annotation
  • Mismatching parameter types with path variables
  • Not explicitly naming @PathVariable when names differ

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes