Bird
0
0

You want to create a REST endpoint to get details of a book by its category and ISBN. Which method signature correctly uses @PathVariable for this URL pattern: /books/{category}/{isbn}?

hard📝 Application Q8 of 15
Spring Boot - REST Controllers
You want to create a REST endpoint to get details of a book by its category and ISBN. Which method signature correctly uses @PathVariable for this URL pattern: /books/{category}/{isbn}?
Apublic String getBook(@PathVariable("category") String cat, @PathVariable("isbn") String id)
Bpublic String getBook(@PathVariable String category, String isbn)
Cpublic String getBook(@PathVariable int category, @PathVariable String isbn)
Dpublic String getBook(@PathVariable String cat, @PathVariable String isbn)
Step-by-Step Solution
Solution:
  1. Step 1: Match URI variables to parameters

    URI has {category} and {isbn}, so method must have two parameters annotated with @PathVariable for both.
  2. Step 2: Use explicit names if parameter names differ

    public String getBook(@PathVariable("category") String cat, @PathVariable("isbn") String id) uses @PathVariable("category") and @PathVariable("isbn") with different parameter names, which is valid.
  3. Step 3: Check types and completeness

    public String getBook(@PathVariable String cat, @PathVariable String isbn) has parameter names that do not match URI variables (cat != category); public String getBook(@PathVariable int category, @PathVariable String isbn) wrongly uses int for category; public String getBook(@PathVariable String category, String isbn) misses @PathVariable on isbn.
  4. Final Answer:

    public String getBook(@PathVariable("category") String cat, @PathVariable("isbn") String id) -> Option A
  5. Quick Check:

    Explicit names allow different parameter names [OK]
Quick Trick: Use @PathVariable("name") if parameter name differs [OK]
Common Mistakes:
  • Using wrong types for path variables
  • Omitting @PathVariable on parameters
  • Not matching URI variable names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes