8

I'm working on a Spring MVC/Webflow Application (version 3.2) and trying to get exception handling working where I can output a custom exception message to a logfile and error.jsp. The problem I'm having is that the Exception Handler is not getting fired. I've created the following class and annotated it "@ControllerAdvice" and put it into the same package as my controller that is throwing the exception:

@ControllerAdvice
public class MyCustomExceptionController {

    @ExceptionHandler(MyCustomException.class)
    public ModelAndView handleMyException(MyCustomException ex) {   
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/error/error");
        modelAndView.addObject("errorId", ex.getErrorId());
        modelAndView.addObject("message", ex.getErrorMessage());        
        return modelAndView;
    }
}

and added the following to the mvc-config File:

<mvc:annotation-driven/>

And included the following in my app-config File:

<context:component-scan base-package="package containing my controllers and MyCustomExceptionController">
        <context:include-filter type="annotation" 
    expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan> 

Any ideas why this isn't working?

user676567
  • 1,119
  • 9
  • 20
  • 39
  • The method `handleMyException()` will only be invoked whenever an unhandled custom exception of your choice occurs (`MyCustomException`). Is this method **not** invoked even after `MyCustomException` is thrown? [This](http://viralpatel.net/blogs/spring-mvc-exception-handling-controlleradvice-annotation/) is the tutorial you may get closer. – Tiny Feb 19 '14 at 15:38
  • I've put a breakpoint on handleMyException and the method never gets invoked even after I throw MyCustomException. – user676567 Feb 19 '14 at 15:42
  • If you were to annotate a method of your choice with the `@InitBinder` annotation in this class, just as an example, was that method invoked? – Tiny Feb 19 '14 at 16:03
  • Hi Tiny, I've added the following test method to the same class but it never gets invoked: @InitBinder public void InitBinder() { System.out.println("In InitBinder method"); } – user676567 Feb 19 '14 at 16:24
  • I have [this](http://pastebin.com/iYpcin96) configuration file (`dispatcher-servlet.xml`). Would you like to compare it with your config file? Apart from that I have specified `` in another file `applicationContext.xml` but this should not be relevant to the concrete problem you're facing. The `@ControllerAdvice` annotation works with these configurations in both Spring 3.2.2 and Spring 4.0.0 GA (**Disclaimer :** I don't know about Spring WebFlow). – Tiny Feb 19 '14 at 17:53
  • Did you ever get this to work? I'm trying to get @ControllerAdvice to fire everytime webflow throws an exception but I cannot seem to figure it out? – Selwyn Sep 03 '14 at 20:33

2 Answers2

8

The <mvc:annotation-driven/> element implicitly registers a ExceptionHandlerExceptionResolver bean. This class has a initExceptionHandlerAdviceCache() method which scans beans in the context to find those whose class type is annotated with @ControllerAdvice.

It does this by first calling ControllerAdviceBean.findAnnotatedBeans(ApplicationContext). Internally, this method uses ApplicationContext#getBeanDefinitionNames(). The javadoc of this method states

Does not consider any hierarchy this factory may participate

To clarify what this means. When you declare a ContextLoaderListener in your deployment descriptor, it loads what we call a root or application ApplicationContext and makes it available in the ServletContext. When you then declare a DispatcherServlet, it creates its own servlet ApplicationContext and uses any ApplicationContext it finds in the ServletContext attributes loaded by the ContextLoaderListener as a parent to that context. The hierarchy looks like so

Root ApplicationContext // loaded by the ContextLoaderListener
            |
Servlet ApplicationContext // loaded by the DispatcherServlet

Every ApplicationContext has access to beans in parent contexts, but not the other way around.

The method above chooses not to use the beans in parent contexts and so only has access to beans in the current ApplicationContext (BeanFactory really).

As such, if your

<context:component-scan .../>

is declared in a root ApplicationContext as I'll assume from the name app-config, but the

<mvc:annotation-driven />

is declared in the servlet ApplicationContext, again assuming from mvc-config, then the ExceptionHandlerExceptionResolver looking for @ControllerAdvice beans will not find any. It is looking for beans in the servlet context but they aren't there, they are in the root context.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

In case anyone else runs into a problem like this - I found an error I had.

I only had one RequestMapping (http://localhost:8080/myapp/verify/verify)

In an InterceptorController, in the PreHandle method, I explicitly threw an exception -> throw new MyCustomException("error","error.jsp") to test my @ControllerAdvice Exception handling.

When I went to http://localhost:8080/myapp/ I would see the interceptor controller get called, my custom exception get thrown, but the @ControllerAdvice class with my @ExceptionHandler(MyCustomException.class) was never called.

I added a @RequestMapping(value="/") and it resolved my issues. Since I was attempting to go to a URI that had no @RequestMapping associated with it, i was getting a 'NoHandlerFoundException' which was shorting out my Exception from bubbling up.

In short, make sure the URI you're attempting to invoke has a @RequestMapping associated with it, or have a method in your ExceptionHandler class to deal with the NoHandlerFoundException.

Hope this helps.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
Brian Beech
  • 1,055
  • 9
  • 13