A. The method attribute should use RequestMethod.POST, not a string.
B. The path attribute cannot be used with @RequestMapping.
C. The method must return void, not String.
D. The method name must be saveDataPost, not saveData.
Solution
Step 1: Check method attribute type
The method attribute expects a RequestMethod enum value, not a string.
Step 2: Identify correct usage
Using method = "POST" is invalid; it should be method = RequestMethod.POST.
Final Answer:
The method attribute should use RequestMethod.POST, not a string. -> Option A
Quick Check:
method attribute needs RequestMethod enum [OK]
Hint: Use RequestMethod.POST enum, not string "POST" [OK]
Common Mistakes:
Passing HTTP method as string instead of enum
Thinking path attribute is invalid
Assuming return type or method name causes error
5. You want to create two methods in a controller: one handles GET requests at "/items" and another handles POST requests at the same path "/items". Which is the best way to do this?
hard
A. Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST).
B. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually.
C. Use @RequestMapping(path = "/items") on both methods without specifying method attribute.
D. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path.
Solution
Step 1: Understand how to map same path with different methods
Spring allows multiple methods mapped to the same path but different HTTP methods using method attribute.
Step 2: Evaluate options
Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). correctly uses two methods with same path but different HTTP methods. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually. is less clean and error-prone. Use @RequestMapping(path = "/items") on both methods without specifying method attribute. causes ambiguity. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path. misses path, so won't map correctly.
Final Answer:
Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). -> Option A
Quick Check:
Separate methods with same path, different HTTP methods [OK]
Hint: Map same path with different methods by specifying method attribute [OK]
Common Mistakes:
Not specifying method attribute causing conflicts
Checking HTTP method manually inside one method
Omitting path attribute causing mapping errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.