1. Which annotation maps an HTTP PUT request to a controller method?
Why:@PutMapping is the shortcut annotation for handling HTTP PUT requests. It works like @RequestMapping(method = RequestMethod.PUT).
2. Which annotation is used to bind the JSON body of a request to a Java object parameter?
Why:@RequestBody tells Spring to read the HTTP request body and convert it into the Java object declared as the method parameter.
3. Which Content-Type header value should a client send when the request body contains JSON?
Why: The application/json media type tells the server that the request body is JSON, so Spring can pick the right converter to read it.
4. What does Spring MVC use to convert a JSON request body into a Java object?
Why:HttpMessageConverter implementations read the request body and write the response body. For JSON, a converter based on a JSON library is used.
5. What must be present on the classpath so that Spring MVC can convert JSON to Java objects?
Why: Spring MVC does not parse JSON by itself. It needs a JSON library like Jackson on the classpath to do the conversion.
6. What does @PathVariable do in the handler method below?
@PutMapping("/{id}")
public String update(@PathVariable("id") long id,
@RequestBody Item item) {
...
}
Why:@PathVariable binds a value from the URL template, such as {id}, to a method parameter.
7. What is an HTTP PUT request normally used for?
Why: PUT is normally used to create or fully update the resource identified by the request URL, and the new data is sent in the request body.
8. A controller method has @ResponseBody and returns a String. What happens to the returned text?
Why: With @ResponseBody, the return value is written to the response body and is not treated as a view name.
9. A handler method uses @RequestBody to read a JSON object. The client sends the JSON with Content-Type: text/plain. What is the most likely result?
Why: Spring picks a converter using the Content-Type header. If no converter supports the media type, it responds with 415 Unsupported Media Type.
10. A controller method is mapped with @PutMapping. A client sends a GET request to the same URL. What is the most likely response?
Why: The URL is mapped only for PUT. A request with a different HTTP method, like GET, gets a 405 Method Not Allowed response.
11. How can a web page send an HTTP PUT request with a JSON body?
Why: HTML forms support only GET and POST. To send PUT with a JSON body, a page can use JavaScript, such as an AJAX call, and set the Content-Type to application/json.
12. When Spring converts a JSON body to a Java object, how are the JSON fields matched with the Java object?
Why: The JSON library maps each JSON field to the Java property with the same name.
13. Which annotation on a Java configuration class enables Spring MVC features such as the default message converters?
Why:@EnableWebMvc turns on the Spring MVC configuration. This includes registering default HttpMessageConverter instances, and the JSON converter is added when a JSON library is on the classpath.