1. What is the main job of an HttpMessageConverter in Spring MVC?
Why: An HttpMessageConverter reads the HTTP request body into a Java object and writes a Java object into the HTTP response body.
2. Why would you create a custom HttpMessageConverter for a format like CSV?
Why: Spring MVC has default converters for common formats like JSON and XML, but not for CSV. For CSV, you need to write and register your own converter.
3. Which base class can you extend to make writing a custom converter easier?
Why:AbstractHttpMessageConverter is a convenient base class. You only need to implement a few methods for reading, writing and type support.
4. Which method of AbstractHttpMessageConverter do you override to say which Java types the converter can handle?
Why: The supports() method returns true for the Java classes the converter can read or write.
5. Which method of AbstractHttpMessageConverter do you override to turn the request body into a Java object?
Why:readInternal() reads the HTTP request body and creates the Java object from it.
6. Which method of AbstractHttpMessageConverter do you override to turn a Java object into the response body?
Why:writeInternal() takes the Java object and writes it to the HTTP response body in the format you want.
7. How does a custom converter tell Spring which content type it supports?
Why: A converter usually passes its supported MediaType, such as text/csv, to the constructor of the parent class.
8. Which request header does Spring use to pick a converter for reading the request body?
Why: The Content-Type header tells Spring the format of the request body. Spring uses it to find a converter that can read that format.
9. Which request header does Spring use to pick a converter for writing the response body?
Why: The Accept header tells Spring which formats the client wants back. Spring uses it, together with the controller's produces setting, to pick a converter for the response.
10. Which annotation tells Spring to convert the request body into a method parameter using a message converter?
Why:@RequestBody makes Spring read the request body and convert it to the parameter type using a matching HttpMessageConverter.
11. Which annotation makes Spring write a method's return value directly to the response body using a message converter?
Why:@ResponseBody tells Spring to skip view resolution and write the return value to the response body using a message converter.
12. Which attribute of @PostMapping or @GetMapping limits a handler method to requests with a given body content type?
Why: The consumes attribute limits the method to requests whose Content-Type matches. For example, consumes = "text/csv" accepts only CSV request bodies.
13. Which attribute of a mapping annotation declares the content type that a handler method returns?
Why: The produces attribute declares the response content type. It also helps Spring choose a converter that can write that type.
14. What do you need to do after writing a custom HttpMessageConverter?
Why: A custom converter must be registered in the MVC configuration. After that, Spring includes it when it chooses converters.
15. Which interface do you implement in a Java configuration class to register custom message converters?
Why:WebMvcConfigurer has callback methods, such as configureMessageConverters(), that let you customize the converters Spring MVC uses.
16. What does Spring do if no registered converter can handle the content type of a request body?
Why: If no converter can read the given content type, Spring cannot process the body. It responds with an error, usually 415 Unsupported Media Type.
17. A third-party library is often used inside a CSV converter. What is the main reason?
Why: Spring only handles the HTTP side. A CSV library does the actual parsing and writing of CSV text, so you don't write that logic yourself.
18. A converter must handle many different bean types, such as a list of employees or a list of products. What is a good way to design it?
Why: A generic converter with a common base type can support many bean types. This avoids writing a new converter for each one.