title: ControllerAdvice not handling exception
body: <p>I have a spring boot 1.3.3 application. I have a rest controller that throws an exception in certain situations. The exceptions are being thrown and the message printed but the ControllerAdvice class is not being fired. Here's my relevant files:
  TestController.java</p>
  
  <pre><code>@Controller
  public class TestController {
      // RESTful method
      @RequestMapping(
              value = "/test",
              method = RequestMethod.POST,
              consumes = "application/json",
              produces = "application/json")
      @ResponseBody
      public JsonNode fooRestTest(@RequestHeader HttpHeaders header,
              @RequestBody String payload) {
          ControllerUtil.validateHeaders(header);
          return null;
      }
  }
  </code></pre>
  
  <p>ControllerUtil.java</p>
  
  <pre><code>public class ControllerUtil {
      public static void validateHeaders(HttpHeaders headers) {
          if(headers.get("test-field") == null) {
              throw new InvalidHeaderException("Invalid test-field in header");
          }
      }
  }
  </code></pre>
  
  <p>InvalidHeaderException.java</p>
  
  <pre><code>public class InvalidHeaderException extends RuntimeException {
  
  
      public InvalidHeaderException(String message) {
          super(message);
      }
  
      public InvalidHeaderException(String message, Throwable cause) {
          super(message, cause);
      }
  }
  </code></pre>
  
  <p>AppErrorController.java</p>
  
  <pre><code>@ControllerAdvice
  @EnableWebMvc
  public class AppErrorController {
  
      @ExceptionHandler(InvalidHeaderException.class)
      public ModelAndView handleHeaderError(HttpServletRequest req, Exception ex) {
          ModelAndView mav = new ModelAndView();
          mav.addObject("exception", ex);
          mav.addObject("url", req.getRequestURL());
          mav.addObject("timestamp", LocalDateTime.now());
          mav.addObject("status", 400);
          return mav;
      }
  }
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;web-app id="RBGApp" version="3.0"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  
      &lt;display-name&gt;FooTester&lt;/display-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Foo_Test&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Mappings --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Foo_Test&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>MvcConfiguration.java</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class MvcConfiguration extends WebMvcConfigurerAdapter {
      @Autowired
      ServletContext servletContext;
  
      @Override
      public void configureDefaultServletHandling(
              DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  
      @Bean
      public ViewResolver getJspViewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("WEB-INF/jsp/views/");
          resolver.setSuffix(".jsp");
          resolver.setOrder(1);
          return resolver;
      }
  }
  </code></pre>
  
  <p>I tested this with a request from postman and it throws the exception and prints out the line:</p>
  
  <pre><code>Invalid test-field in header
  </code></pre>
  
  <p>But the code never goes into the <code>AppErrorController</code> at all. So none of the other values like <code>timestamp</code> are populated. Any idea what i'm doing wrong here?</p>
  
  <p><strong>Edit 1:</strong>
  This is what postman spits out as the response:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Apache Tomcat/7.0.62 - Error report&lt;/title&gt;
          &lt;style&gt;
              &lt;!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--&gt;
          &lt;/style&gt; 
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;HTTP Status 500 - Invalid test-field in header&lt;/h1&gt;
          &lt;HR size="1" noshade="noshade"&gt;
          &lt;p&gt;
              &lt;b&gt;type&lt;/b&gt; Exception report
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;message&lt;/b&gt;
              &lt;u&gt;Invalid test-field in header&lt;/u&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;description&lt;/b&gt;
              &lt;u&gt;The server encountered an internal error that prevented it from fulfilling this request.&lt;/u&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;exception&lt;/b&gt;
              &lt;pre&gt;controllers.exceptions.InvalidHeaderException: Invalid test-field in header
      controllers.util.ControllerUtil.validateHeaders(ControllerUtil.java:12)
      controllers.TestController.fooRestTest(TestController.java:77)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:497)
      org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:120)
      org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
      org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:95)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
  &lt;/pre&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;note&lt;/b&gt;
              &lt;u&gt;The full stack trace of the root cause is available in the Apache Tomcat/7.0.62 logs.&lt;/u&gt;
          &lt;/p&gt;
          &lt;HR size="1" noshade="noshade"&gt;
          &lt;h3&gt;Apache Tomcat/7.0.62&lt;/h3&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Edit 2:</strong> Console output when making the postman request</p>
  
  <pre><code>09:08:14.373 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Foo_Test' processing POST request for [/TEST]
  09:08:14.381 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test
  09:08:14.390 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public com.fasterxml.jackson.databind.JsonNode controllers.AlertController.getAlertsForRest(org.springframework.http.HttpHeaders,java.lang.String)]
  09:08:14.390 [http-bio-8080-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'alertController'
  09:08:14.426 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read [class java.lang.String] as "application/json" with [org.springframework.http.converter.StringHttpMessageConverter@4732b26d]
  09:08:14.436 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.fasterxml.jackson.databind.JsonNode controllers.AlertController.getAlertsForRest(org.springframework.http.HttpHeaders,java.lang.String)]: controllers.exceptions.InvalidHeaderException: Invalid test-field in header
  09:08:14.439 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public com.fasterxml.jackson.databind.JsonNode controllers.AlertController.getAlertsForRest(org.springframework.http.HttpHeaders,java.lang.String)]: controllers.exceptions.InvalidHeaderException: Invalid test-field in header
  09:08:14.439 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.fasterxml.jackson.databind.JsonNode controllers.AlertController.getAlertsForRest(org.springframework.http.HttpHeaders,java.lang.String)]: controllers.exceptions.InvalidHeaderException: Invalid test-field in header
  09:08:14.443 [http-bio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
  controllers.exceptions.InvalidHeaderException: Invalid test-field in header
      controllers.util.ControllerUtil.validateHeaders(ControllerUtil.java:12)
      controllers.TestController.fooRestTest(TestController.java:77)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:120)
      at org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
      at org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:95)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>