Announcement Announcement Module
Collapse
No announcement yet.
Spring Web MVC: @ExceptionHandler ignore when exceptionResolver in place Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Web MVC: @ExceptionHandler ignore when exceptionResolver in place

    Hello,

    I've run into an issue trying to use the @ExceptionHandler annotation in a controller class: I want a specific exception to be handled by a method annotated with @ExceptionHandler, but still would want to handle most exceptions in a generic way: for this I'm using SimpleMappingExceptionResolver.

    But with the exceptionResolver in place, it seems the @ExceptionHandler methods are never invoked!

    I'm wondering if this behaves as intended, and if its a bug in the framework? My main concern is cleanly separating controller-specific exception handling from general exception handling, and can't think of any feasible workarounds.

    Please tell me if you know of any good solutions to this issue.

    Thanks for your help!

  • #2
    I'm having exactly the same problem. Did you find a solution and/or answer to your question?

    Comment


    • #3
      Solution found

      Background

      After some digging in the code (God bless Open Source and Spring Team for making SF Open Sourced!) I figured out why is this happening.

      If Dispatcher Servlet is configured to detect all exception resolvers found in application context (see DispatcherServlet.java:421), which is true by default, it fetches all the Exception Resolvers and sorts them DispatcherServlet.java:428) since standard resolvers implement Ordered interface.

      Then, whenever it finds out that no exception handlers were configured (see DispatcherServlet.java:443) it installs default ones according to default strategy. These are AnnotationMethodHandlerExceptionResolver, ResponseStatusExceptionResolver and DefaultHandlerExceptionResolver; I'm not sure about their ordering.

      Explanation

      So what (probably) happened in your case?

      First you tried @ExceptionHandler annotations without configuring any Exception Resolvers in application context. It worked since DispatcherServled installed AnnotationMethodHandlerExceptionResolver for you.

      Then you added SimpleMappingExceptionResolver to application context. Dispatcher Servlet thought that since you put Exception Resolver in application context you know what you are doing, so it didn't add any other Exception Resolvers.

      My solution

      I ran into the same problem with Spring Roo generated app.

      Here's what was generated by Roo:
      HTML Code:
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException">
        <property name="exceptionMappings">
          <props>
            <prop key=".DataAccessException">dataAccessFailure</prop>
            <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
            <prop key=".TypeMismatchException">resourceNotFound</prop>
            <prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
          </props>
        </property>
      </bean>
      Here's what I've changed it to:
      HTML Code:
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1" />
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="2" p:defaultErrorView="uncaughtException">
        <property name="exceptionMappings">
          <props>
            <prop key=".DataAccessException">dataAccessFailure</prop>
            <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
            <prop key=".TypeMismatchException">resourceNotFound</prop>
            <prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
          </props>
        </property>
      </bean>
      Note that not only I added AnnotationMethodHandlerExceptionResolver to the context I also explicitly set ordering in order to put SimpleMappingExceptionResolver as a catchall.

      Comment


      • #4
        Actually haven't looked at the issue since I posted it here, and was pretty glad to see your elaborate response and solution.

        Thanks!

        Comment

        Working...
        X