Results 1 to 6 of 6

Thread: Exception Hander annotation and interceptor in Spring

  1. #1
    Join Date
    Oct 2011
    Posts
    15

    Default 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

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    @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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2011
    Posts
    15

    Default

    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

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    That might work but effectively that would duplicate ExceptionHandler methods.

    Which interceptor are we talking about here, aop or a HandlerInterceptor.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Oct 2011
    Posts
    15

    Default

    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

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

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
  •