Bird
0
0

Consider this controller method:

medium📝 Debug Q14 of 15
Spring Boot - Request and Response Handling
Consider this controller method:
@GetMapping("/books/{bookId}")
public String getBook(@PathVariable int bookId, @RequestParam int page) {
    return "Book: " + bookId + ", Page: " + page;
}

What error will occur if a client calls /books/abc?page=2?
A404 Not Found because path variable missing
B500 Internal Server Error due to null page
C400 Bad Request due to type mismatch
DNo error, returns "Book: abc, Page: 2"
Step-by-Step Solution
Solution:
  1. Step 1: Check path variable type

    The method expects bookId as int, but URL provides "abc" which is not a number.
  2. Step 2: Understand Spring Boot behavior on type mismatch

    Spring Boot returns 400 Bad Request when it cannot convert path variable to required type.
  3. Final Answer:

    400 Bad Request due to type mismatch -> Option C
  4. Quick Check:

    Type mismatch in @PathVariable causes 400 error [OK]
Quick Trick: Non-numeric path variable for int causes 400 error [OK]
Common Mistakes:
  • Expecting 404 Not Found instead of 400
  • Assuming null page causes error
  • Thinking method returns string with invalid input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes