In a Spring MVC application, you can register one or more exception resolver beans in the web application context to resolve uncaught exceptions. These beans have to implement the
HandlerExceptionResolver
interface for DispatcherServlet
to auto-detect them. Spring MVC comes with a simple exception resolver for you to map each category of exceptions to a view i.e. SimpleMappingExceptionResolver
to map each category of exceptions to a view in a configurable way.
Let’s say we have an exception class i.e.
AuthException
. And we want that everytime this exception is thrown from anywhere into application, we want to show a pre-determined view page /WEB-INF/views/error/authExceptionView.jsp
. So the configuration would be.< bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > < property name = "exceptionMappings" > < props > < prop key = "com.howtodoinjava.demo.exception.AuthException" > error/authExceptionView </ prop > </ props > </ property > < property name = "defaultErrorView" value = "error/genericView" /> </ bean > |
The “defaultErrorView” property can be configured to show a generic message for all other exceptions which are not configured inside “exceptionMappings” list.
No comments:
Post a Comment