Bird
0
0

Given this method in a Spring Boot controller:

medium📝 component behavior Q4 of 15
Spring Boot - REST Controllers
Given this method in a Spring Boot controller:
@PutMapping("/users/{id}")
public ResponseEntity updateUser(@PathVariable int id, @RequestBody User user) {
    return ResponseEntity.ok("User " + id + " updated");
}

What will be the response body when a PUT request is sent to /users/5?
A"User updated"
B"User 5 updated"
C"5 updated"
DEmpty response
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the method return statement

    The method returns ResponseEntity.ok with a string concatenating "User ", the id, and " updated".
  2. Step 2: Substitute the path variable value

    When the PUT request is to /users/5, id = 5, so the response body is "User 5 updated".
  3. Final Answer:

    "User 5 updated" -> Option B
  4. Quick Check:

    Response body includes ID = "User 5 updated" [OK]
Quick Trick: PathVariable values appear in response if used in return [OK]
Common Mistakes:
  • Ignoring path variable in response
  • Assuming empty response
  • Confusing with request body content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes