1. What is the main job of SimpleMappingExceptionResolver?
Why:SimpleMappingExceptionResolver is a HandlerExceptionResolver that lets you configure which view should be shown for which exception class.
2. Which interface does SimpleMappingExceptionResolver implement (directly or through its parent class)?
Why:SimpleMappingExceptionResolver is an implementation of HandlerExceptionResolver, the Spring MVC interface used to resolve exceptions thrown by handlers.
3. Which method is used to set the default view shown when an exception has no specific mapping?
Why:setDefaultErrorView(String) sets the view name used for any exception that is not matched by the exception mappings.
4. Which method is used to set the HTTP status code that is sent when the default error view is shown?
Why:setDefaultStatusCode(int) defines the response status code used together with the default error view.
5. What does addStatusCode("myView", 404) do?
Why:addStatusCode links a view name to a response status code. When that view is chosen for an exception, the status is applied to the response.
6. What type of object is passed to setExceptionMappings() to map exception class names to view names?
Why:setExceptionMappings(Properties) takes a Properties object where each key is an exception class name and each value is a view name.
7. Which code correctly registers a SimpleMappingExceptionResolver in a Java config class?
@EnableWebMvc
@Configuration
public class AppConfig
implements WebMvcConfigurer {
@Override
public void ______(
List<HandlerExceptionResolver> list) {
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
list.add(r);
}
}
Why:WebMvcConfigurer provides the configureHandlerExceptionResolvers callback. Resolvers added to the list are used by Spring MVC.
8. What will be the view name used for a NullPointerException in this configuration?
Properties p = new Properties();
p.setProperty(
NullPointerException.class.getName(),
"npeView");
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.setDefaultErrorView("errorView");
Why: A mapping exists for NullPointerException, so its mapped view npeView is used. The default view is only used when no mapping matches.
9. A handler throws IllegalStateException. What view and status code are used in this configuration?
Properties p = new Properties();
p.setProperty(
NullPointerException.class.getName(),
"npeView");
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.addStatusCode("npeView", 404);
r.setDefaultErrorView("errorView");
r.setDefaultStatusCode(400);
Why:IllegalStateException is not mapped, so the default error view and the default status code are used.
10. A handler throws NullPointerException. What view and status code are used in this configuration?
Properties p = new Properties();
p.setProperty(
NullPointerException.class.getName(),
"npeView");
SimpleMappingExceptionResolver r =
new SimpleMappingExceptionResolver();
r.setExceptionMappings(p);
r.addStatusCode("npeView", 404);
r.setDefaultErrorView("errorView");
r.setDefaultStatusCode(400);
Why: The exception is mapped to npeView, and that view has its own status code 404, so the default status code is not used.
11. In a view page, which expression shows the exception object by default?
Why:NumberFormatException is thrown by parseInt. The resolver finds its mapping and renders badIdView.
15. Which statement about the exception mapping is correct?
Why: The mapping keys are class names, so both JDK and custom exceptions can be mapped by their full class names. Many mappings can be added to one resolver.