Bird
0
0

Analyze the following Spring Boot controller method:

medium📝 component behavior Q4 of 15
Spring Boot - REST Controllers
Analyze the following Spring Boot controller method:
@GetMapping("/invoice/{invoiceId}")
public String showInvoice(@PathVariable String invoiceId) {
    return "Invoice Number: " + invoiceId;
}

What will be the response when a client accesses /invoice/7890?
A"Invoice Number: null"
B"Invoice Number: {invoiceId}"
C"Invoice Number: 7890"
DHTTP 404 Not Found error
Step-by-Step Solution
Solution:
  1. Step 1: URI Template Matching

    The URL /invoice/7890 matches the pattern /invoice/{invoiceId} with invoiceId=7890.
  2. Step 2: @PathVariable Binding

    The method parameter invoiceId is bound to the string "7890".
  3. Step 3: Return Value

    The method returns "Invoice Number: " concatenated with invoiceId, resulting in "Invoice Number: 7890".
  4. Final Answer:

    "Invoice Number: 7890" -> Option C
  5. Quick Check:

    URL matches pattern and variable binds correctly [OK]
Quick Trick: Path variable value replaces placeholder in URL [OK]
Common Mistakes:
  • Expecting literal {invoiceId} in output
  • Assuming null or error without matching URI

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes