Bird
0
0

Which of the following is the correct syntax to return a 201 Created status with a ResponseEntity in Spring Boot?

easy📝 Syntax Q3 of 15
Spring Boot - Request and Response Handling
Which of the following is the correct syntax to return a 201 Created status with a ResponseEntity in Spring Boot?
Areturn ResponseEntity.status(201, object);
Breturn new ResponseEntity<>(object, HttpStatus.CREATED);
Creturn ResponseEntity.created(object);
Dreturn new ResponseEntity<>(HttpStatus.CREATED, object);
Step-by-Step Solution
Solution:
  1. Step 1: Check ResponseEntity constructor usage

    The constructor takes the body first, then the status code, so new ResponseEntity<>(object, HttpStatus.CREATED) is correct.
  2. Step 2: Analyze other options

    B is invalid syntax as status() takes only one argument; C is incorrect because created() expects a URI; D has parameters reversed.
  3. Final Answer:

    return new ResponseEntity<>(object, HttpStatus.CREATED); -> Option B
  4. Quick Check:

    Constructor order: body, status [OK]
Quick Trick: ResponseEntity constructor: body first, then status [OK]
Common Mistakes:
  • Swapping parameters in ResponseEntity constructor
  • Using created() without URI argument
  • Confusing builder and constructor syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes