1. What is the job of an HttpMessageConverter in Spring MVC?
Why: An HttpMessageConverter reads the body of an HTTP request into a Java object, and writes a Java object into the body of an HTTP response.
2. Which part of an HTTP message does an HttpMessageConverter work with?
Why: Message converters work with the body of the request and the response. They are used with @RequestBody and @ResponseBody.
3. When do you need to create a custom HttpMessageConverter?
Why: Spring MVC already has converters for common formats like JSON, XML and plain text. You create a custom converter for any other format.
4. Which class is a convenient base class for creating a custom message converter?
Why:AbstractHttpMessageConverter is a base class that makes it easier to write your own converter. You can also implement HttpMessageConverter directly, but that needs more code.
5. Why is it usually better to extend AbstractHttpMessageConverter than to implement HttpMessageConverter directly?
Why: The base class removes repeated plumbing code. For example, it supports a list of media types and sets the Content-Type and Content-Length headers on output.
6. In a class that extends AbstractHttpMessageConverter, which method tells Spring which Java classes the converter can handle?
Why: You override supports(Class<?> clazz) and return true if the converter can handle that Java type.
7. In a class that extends AbstractHttpMessageConverter, which method do you override to read the request body and create a Java object?
Why:readInternal(Class<?> clazz, HttpInputMessage inputMessage) receives the input message. Your code reads the body from it and builds the Java object.
8. In a class that extends AbstractHttpMessageConverter, which method do you override to write a Java object to the response body?
Why:writeInternal(T t, HttpOutputMessage outputMessage) receives the Java object and the output message. Your code writes the object to the output message body.
9. What do the supported media types of a message converter tell Spring?
Why: Media types, such as application/json or a custom type, tell Spring which Content-Type values the converter can handle.
10. How does Spring choose a converter to read a request body?
Why: Spring checks each converter and uses the first one that supports both the request's Content-Type and the target Java type.
11. Which annotation on a controller method parameter tells Spring to convert the request body into a Java object using a message converter?
Why:@RequestBody tells Spring to read the request body and convert it with a matching HttpMessageConverter.
12. Which annotation tells Spring to write the return value of a controller method to the response body using a message converter?
Why:@ResponseBody tells Spring to convert the return value with a matching HttpMessageConverter and write it to the response body instead of looking for a view.
13. Which WebMvcConfigurer method is overridden to register a custom message converter?
Why: In a @Configuration class that implements WebMvcConfigurer, you override configureMessageConverters() and add your converter there.
14. Which attribute of @RequestMapping limits a handler method to requests with a specific Content-Type?
Why: The consumes attribute limits the handler to requests with the given Content-Type. It is useful with a custom media type handled by your own converter.
15. What should the supports() method of a custom converter usually check?
Why: A converter is made for a specific Java type. supports() returns true only when the given class is that type (or a subclass of it).