Bird
0
0

Identify the error in this Spring Boot controller method that tries to return a 201 Created status:

medium📝 Debug Q14 of 15
Spring Boot - Request and Response Handling
Identify the error in this Spring Boot controller method that tries to return a 201 Created status:
public ResponseEntity createItem() {
    return new ResponseEntity<>("Item created", 201);
}
ACannot return ResponseEntity with a String body
BUsing int 201 instead of HttpStatus enum
CResponseEntity constructor requires three arguments
DMissing @ResponseBody annotation
Step-by-Step Solution
Solution:
  1. Step 1: Check ResponseEntity constructor parameters

    The constructor expects a body and an HttpStatus enum, not an int.
  2. Step 2: Identify the incorrect use of status code

    Passing 201 as int causes a compilation error; should use HttpStatus.CREATED instead.
  3. Final Answer:

    Using int 201 instead of HttpStatus enum -> Option B
  4. Quick Check:

    Use HttpStatus.CREATED, not int 201 [OK]
Quick Trick: Use HttpStatus enum, not int, for status codes [OK]
Common Mistakes:
  • Passing int instead of HttpStatus enum
  • Forgetting to import HttpStatus
  • Assuming String body is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes