Reputation: 4201
package com.beebunny.springapp.exception.resolvers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
public class MyExceptionResolver extends SimpleMappingExceptionResolver {
private static final String ERROR_PAGE = "path/to/error/page.html";
@Override
protected ModelAndView doResolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
ModelAndView mav = new ModelAndView(ERROR_PAGE);
return mav;
}
}
I have a controller that I wrote to just throw an exception. I am expecting the class above to have its doResolveException get trigged. I need to do things in here such as logging, call an API, and some other custom handling.
Unfortunately, the version of Spring I'm working with doesn't support @ControllerAdvice, and so I'm falling back to SimpleMappingExceptionResolver.
My requirement is that I need an exception handler that will catch any uncaught exceptions if something blows up for any reason.
Not sure what the common reasons are for this to not work.
I do have context:component-scan defined in my bean config with the base-package containing the package for my exception handler implementation above.
Edit: Here is the relevant line from my bean config:
<context:component-scan base-package="com.beebunny.springapp"/>
Upvotes: 0
Views: 1223
Reputation: 4201
The solution is two-fold.
Instead, I had to add a property to MyExceptionResolver and explicitly specify this bean in my Spring config. So to MyExceptionResolver, I added this property.
private int order;
And then, as I said, I had to explicitly define this bean, and I had to manually specify a value for this order property, as so:
<bean id="exceptionResolver" class="com.beebunny.springapp.exception.resolvers.MyExceptionResolver">
<property name="order" value="0"/>
</bean>
In other words, my project had a default exception resolver, and I just needed to override the order that the exception resolvers are used in.
Upvotes: 3