1. What is the main job of a custom Converter in Spring MVC data binding?
Why: A custom converter takes a value from the request (usually a String, such as a URI template variable) and turns it into a Java object that can be passed to a handler method.
2. Which Spring feature can be used instead of PropertyEditor for String to type conversion?
Why: Instead of using PropertyEditor, Spring MVC can use the conversion service (ConversionService) for converting Strings to other types.
3. How many ConversionService instances does Spring MVC use per container?
Why: Spring MVC uses a single instance of the ConversionService interface per container. All registered converters are managed by it.
4. Which interface should a class implement to create a custom converter?
public class MyConverter implements ______ {
...
}
Why: A custom converter implements org.springframework.core.convert.converter.Converter<S, T>.
5. In Converter<S, T>, what do S and T mean?
Why:S is the type you convert from (source) and T is the type you convert to (target).
6. Which method of the Converter interface performs the actual conversion?
public class MyConverter implements Converter {
@Override
public MyType ______(String source) {
...
}
}
Why: The Converter interface has one method, convert(), which takes the source value and returns the target object.
7. What is the usual source type when a converter handles a URI template variable?
Why: Values in the URL, like URI template variables, arrive as text. So the source type of the converter is usually String.
8. What is one extra benefit of a custom converter compared to Spring's implicit mapping of request values to object properties?
Why: With implicit mapping, Spring only copies values into properties. A custom converter can run your own logic, such as loading the object from a database or adding extra data.
9. Which interface defines callback methods to customize the Java-based configuration of Spring MVC?
Why:WebMvcConfigurer defines callback methods to customize the Java-based configuration for Spring MVC enabled with @EnableWebMvc.
10. Which annotation enables Spring MVC Java-based configuration?
@Configuration
______
public class AppConfig implements WebMvcConfigurer {
...
}
Why:@EnableWebMvc turns on the Spring MVC configuration, which can then be customized with WebMvcConfigurer.
11. Which WebMvcConfigurer method is used to register custom converters and formatters?
@Override
public void ______(FormatterRegistry registry) {
...
}
Why:addFormatters(FormatterRegistry registry) is the method used to add converters and formatters.
12. Which method of FormatterRegistry is used to register a custom converter?
registry.______(new MyConverter());
Why: A converter is registered by calling addConverter() on the FormatterRegistry.
13. Do converters added with addFormatters() replace the default converters?
Why: The addFormatters() method adds your converters and formatters in addition to the ones Spring registers by default.
14. When using @PathVariable without an explicit name, what must match for Spring to resolve the URI template variable to the method parameter?
Why: If you specify @PathVariable("id"), the parameter name can differ; conversion is then applied to the resolved path-variable value.
15. What happens If a @PathVariable has no explicit name and no URI template variable matches the method parameter name?
Why: When @PathVariable is used without an explicit name (e.g., @PathVariable String id), Spring falls back to matching the method parameter name against the URI template variables. If no variable in the URI template matches that name, Spring throws a MissingPathVariableException (resulting in an HTTP 500 Internal Server Error by default).
16. When a converter creates the backing object for a handler method parameter, what does Spring do with that object before the handler method is called?
Why: Spring implicitly adds the backing object to the Model before the handler method is called, so the view can use it directly.