Spring 3.2 @ControllerAdvice Not Working

If you use classpath scanning, probably you have to add new include filter to your <context:component-scan> element:

<context:include-filter type="annotation" 
    expression="org.springframework.web.bind.annotation.ControllerAdvice" />

Default scanning does not lookup this annotation, following spring-context-3.2.xsd for component-scan:

"Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected."


I had this same problem, in my case the problem was that there was a dependent library that had inside it a class with the @ControllerAdvice and @Order(Ordered.HIGHEST) annotation, to solve the problem I added the @Order(Ordered.HIGHEST) annotation in my classe, and now it works.

Since my exception class is in the same controller package spring gave my class higher priority even though both classes have the same @Order(Ordered.HIGHEST)

@ControllerAdvice
@Order(Ordered.HIGHEST)
public class RestResponseEntityExceptionHandler 
  extends ResponseEntityExceptionHandler { 

For this problem, The first thing is confirming your config,

  1. You need make sure that the @ControllerAdvice Class under your component-scan base package.
  2. Make suer you use <mvc:annotation-driven/> in your spring-servlet.xml. or have @EnableWebMvc in your @ControllerAdvice Class

When you have the config right, the ControllerAdvice should already work, Now you said You got your uncaught exception handler view. I guess you got that in your InegrationTest, And you used mockMvc to test that, If so, you need put @WebAppConfiguration and build mokcMvc as follow:

 @Autowired
private WebApplicationContext wac;

mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();

Using standaloneSetup(controller) will not work because lack of WebApplicationContext.