-
Oct 2nd, 2011, 06:31 PM
#1
Exception Hander annotation and interceptor in Spring
Hi,
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
Thanks
Rams
-
Oct 3rd, 2011, 02:03 AM
#2
@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
#3
Hi Marten,
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.
Thanks
Rams
-
Oct 3rd, 2011, 02:48 PM
#4
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
#5
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.
Thanks
-
Oct 4th, 2011, 12:49 AM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules