Oct 2nd, 2011, 06:31 PM
Exception Hander annotation and interceptor in Spring
We have a Spring MVC app with hibernate as dao. I'm planning to have a single interceptor for exception handling and trying to use @ExceptionHandler annotations or ExceptionResolver interface to handle. But I'm not sure how to use a single inerceptor along with exception handler resolvers at one place in the application.
I'll have nearly 20 controllers but want to handle excpetions with interceptors and annotations in a single class.
please suggest with any sample code
Oct 3rd, 2011, 02:03 AM
@ExceptionHandler can be used on methods inside controllers and they only work for a single (the current executing) controller. So creating a single class with @ExceptionHandler annotated methods isn't going to work.
Oct 3rd, 2011, 01:45 PM
Thanks for your reply. If I have a base controller, can the exceptionHandler be used in it, so that all exceptions be handled at one place? Also, please suggest how can an interceptor be used for exception handling in this case.
Oct 3rd, 2011, 02:48 PM
That might work but effectively that would duplicate ExceptionHandler methods.
Which interceptor are we talking about here, aop or a HandlerInterceptor.
Oct 3rd, 2011, 06:40 PM
I wish I try both aop or HandlerInterceptor. But I belive afterCompletion() should be implemented to handle exception when HandlerInterceptor is used which can handle only one type of exception, isn't it? Btw, which AOP class can be used as exception interceptor. Please suggest.
Oct 4th, 2011, 12:49 AM
With a HandlerInterceptor you cannot handle exceptions, the request is basically already over and you cannot redirect/forward etc. to another view because well that already happened. Handling exceptions is for a HandlerExceptionResolver (that is the whole purpose of that interface/class).
You could use an after throwing advice in AOP to handle exceptions but that will need you to enable proxy creation on controllers, which in turn can be problematic to detect @RequestMapping etc.
Tags for this Thread