Spring SimpleMappingExceptionResolver will not display by default

I play with SimpleMappingExceptionResolver to see how it works and see if it will be used for the client, but I have problems understanding it.

What I tried was visiting a specific page in my application and its exception in the handleRequestInternal method.

When I throw a RecoverableDataAccessException (subclass of DataAccessException), then the correct error page is displayed, as expected.

When I throw a freemarker.core.InvalidReferenceException or java.lang.NumberFormatException, then the exception creates bubbles on the page and the 500 error page is displayed by default (i.e. not styling).

Below is the mapping I am using.

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.dao.DataAccessException">/general/error/500</prop> <prop key="freemarker.core.InvalidReferenceException">/general/error/500</prop> <prop key="NumberFormatException">/general/error/500</prop> </props> </property> <property name="defaultErrorView" value="/general/error/500" /> </bean> 

I, at least, expected that the default error view would allow me to throw an exception and display my specific error page, but this does not happen.

Am I using SimpleMappingExceptionHandler correctly here?

[edit] I am using the pier.

[edit] I realized that SMER does not handle errors that occur during rendering, which explains why it cannot catch those that I am having problems with. Is it possible to get SMER to handle 500 style errors?

+3
source share
1 answer

As you discovered, SimpleMappingExceptionResolver will not work for exceptions that have been selected at the view level. It implements the HandlerExceptionResolver interface, which, as the name implies, handles only exceptions thrown by the handler (i.e., the Controller).

If you need to handle the exceptions thrown by the view, you can write HandlerInterceptor by overriding the afterCompletion() method or write the Spring Filter servlet. In both cases, you will not get the benefits of Spring view resolver, you will have to handle the rendering yourself.

One word of warning, though - when you receive exceptions at the presentation level, it is not always possible to handle them gracefully, since the server may have started to wash out the output of the presentation to the client when an exception occurs, in which if the server cannot display a separate page with an error. That is why it is good practice to make as much logic as you can in the controller layer so that exceptions are caught as early as possible.

+9
source

All Articles