Bird
0
0

Given the following Spring Boot controller method, what will happen when a PUT request is sent to /users/5 with a valid JSON body?

medium📝 component behavior Q13 of 15
Spring Boot - REST Controllers
Given the following Spring Boot controller method, what will happen when a PUT request is sent to /users/5 with a valid JSON body?
@PutMapping("/users/{id}")
public User updateUser(@PathVariable int id, @RequestBody User user) {
    user.setId(id);
    return userService.save(user);
}
AThe user with ID 5 will be updated with the JSON data and returned
BA new user will be created ignoring the ID in the URL
CThe request will fail because @RequestBody is missing
DThe user ID will not be set and cause an error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method parameters and annotations

    The method uses @PutMapping with path variable id and @RequestBody user object. It sets user ID from path.
  2. Step 2: Understand method behavior on PUT request

    It updates the user ID to match URL, then calls save to update and returns updated user.
  3. Final Answer:

    The user with ID 5 will be updated with the JSON data and returned -> Option A
  4. Quick Check:

    PUT updates resource with URL ID and JSON body [OK]
Quick Trick: PUT with @RequestBody updates resource using URL ID [OK]
Common Mistakes:
  • Assuming PUT creates new resource instead of updating
  • Ignoring @PathVariable ID and using JSON ID only
  • Missing @RequestBody causing request failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes