Why:PATCH is used to change only some parts of an existing resource. For example, you can update just one field instead of sending the whole object.
2. Which annotation should be placed on the controller method so that it handles HTTP PATCH requests?
@Controller
@RequestMapping("/items")
public class ItemController {
// annotation here
@ResponseBody
public String update(@RequestBody Item item) {
return "Done";
}
}
Why:@PatchMapping is the shortcut annotation for mapping HTTP PATCH requests to a handler method. The other names do not exist in Spring.
3. What is the main benefit of using mock web objects to test a Spring MVC controller?
Why: Mock web objects simulate requests and responses in memory. The test runs fast and does not need a real server.
4. A test class has an autowired WebApplicationContext named wac. Which statement creates a MockMvcTester from it?
Why:@RequestBody tells Spring to read the request body and convert it to the parameter type, for example from JSON or XML.
12. Which attribute of @PatchMapping limits a handler method to requests with the form data content type?
@PatchMapping(
value = "/{id}",
____ = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseBody
public String update(@RequestBody MultiValueMap params) {
return "Updated";
}
Why:consumes narrows the mapping by the request Content-Type. The handler is used only when the request body has that type.
13. A request has the content type application/x-www-form-urlencoded. Which parameter type with @RequestBody can hold all the form fields?
Why: Form data can have several values for the same field name. MultiValueMap<String, String> stores each field name with a list of its values.
14. Two controller methods use the same URL and both handle PATCH. When can Spring still choose the correct one?
Why: Spring selects the handler by comparing request details such as the Content-Type header with the consumes attribute. Method names and return types are not used for mapping.
15. What is the response body text in this test? Assume Item has a name property with a getter and a setter.