title: @ControllerAdvice and @ExceptionHandler not getting triggered for my RestController
body: <p>In order to have unified exception handling throughout the application I am using <a href="https://www.baeldung.com/exception-handling-for-rest-with-spring" rel="nofollow noreferrer">Error Handling for REST with Spring</a> solution#3 which uses <code>@ControllerAdvice</code> along with <code>@ExceptionHandler</code>.</p>
  
  <p>Spring version: 4.3.22.RELEASE</p>
  
  <p>Spring Boot version: 1.5.19.RELEASE</p>
  
  <p>This is a Spring boot application and following is my package structure.</p>
  
  <pre><code>src/main/java
    com.test.app.controller
       MyRestController.java       -- This is my Rest controller
    com.test.app.handler
       RestExceptionHandler.java   -- This is my ControllerAdvice class
  </code></pre>
  
  <p>Following is my ControllerAdvice code and one of the Controller throws <code>InvalidDataException</code> but still the corresponding <code>@ExceptionHandler</code> is not called. Instead I am getting <code>Unexpected 'e'</code> as the response body with <em>http 400</em>.</p>
  
  <pre><code>@ControllerAdvice
  public class RestExceptionHandler {
  
      @ExceptionHandler(InvalidDataException.class)
      @ResponseStatus(value = HttpStatus.BAD_REQUEST)
      public @ResponseBody ErrorResponse handleValidationError(final InvalidDataException ex,
                                                               final WebRequest request) {
          log.error("InvalidDataException message:{} ", ex.getMessage());
          return getExceptionResponse("Failed with Invalid data" + ex.getMessage(), HttpStatus.BAD_REQUEST.value());
      }
  
  
      private ErrorResponse getExceptionResponse(final String message, final Integer errorCode) {
          final ErrorResponse exceptionResponse = new ErrorResponse();
          exceptionResponse.setErrorCode(errorCode.toString());
          exceptionResponse.setErrorDescription(message);
          log.error("message:{}", exceptionResponse);
          return exceptionResponse;
      }
  }
  </code></pre>
  
  <p>I looked at other post on SO as well as other forums where they mentioned to use <code>@EnableWebMvc</code> and <code>@ComponentScan</code> etc. but nothing helped. Could someone please help me understand what am I missing?</p>
  
  <p>Following is my <code>Controller</code> and corresponding interface.</p>
  
  <pre><code>@RestController
  public class MyRestController implements MyApi {
  
      @Override
      public ResponseEntity&lt;List&lt;MyResponse&gt;&gt; myGet(@RequestHeader(value = "a") String a,
                                                                 @RequestHeader(value = "b") String b,
                                                                 @RequestHeader(value = "c") String c,
                                                                 @RequestHeader(value = "d") String d,
                                                                 @RequestHeader(value = "e") String e,
                                                                 @RequestHeader(value = "f") String f,
                                                                 @RequestHeader(value = "g") String g) {
  
        List&lt;MyResponse&gt; responses = service.getData(c, d, e, f); // This throws exception
        return new ResponseEntity&lt;&gt;(responses, HttpStatus.OK);
      }
  }
  
  
  @Validated
  @Api(value = "My", description = "the My API")
  //This is generated interface through swagger codegen
  public interface MyApi {
  
      @ApiOperation(value = "", nickname = "myGet", notes = "", response = MyResponse.class, responseContainer = "List")
      @ApiResponses(value = {
          @ApiResponse(code = 200, message = "normal response", response = MyResponse.class, responseContainer = "List"),
          @ApiResponse(code = 400, message = "Request is invalid", response = ErrorResponse.class),
          @ApiResponse(code = 401, message = "", response = ErrorResponse.class),
          @ApiResponse(code = 404, message = "", response = ErrorResponse.class),
          @ApiResponse(code = 405, message = "", response = ErrorResponse.class),
          @ApiResponse(code = 409, message = "", response = ErrorResponse.class),
          @ApiResponse(code = 500, message = "Internal Server Error", response = ErrorResponse.class),
          @ApiResponse(code = 503, message = "Service Unavailable", response = ErrorResponse.class) })
      @RequestMapping(value = "/v1/test",
          produces = { "application/json" }, 
          method = RequestMethod.GET)
      default ResponseEntity&lt;List&lt;MyResponse&gt;&gt; myGet(@ApiParam(value = "a" ,required=true) @RequestHeader(value="a", required=true) String a,
                                                     @ApiParam(value = "b" ,required=true) @RequestHeader(value="b", required=true) String b,
                                                     @ApiParam(value = "c" ,required=true) @RequestHeader(value="c", required=true) String c,
                                                     @ApiParam(value = "d" ,required=true) @RequestHeader(value="d", required=true) String d,
                                                     @ApiParam(value = "e" ,required=true) @RequestHeader(value="e", required=true) String e,
                                                     @ApiParam(value = "f" ,required=true) @RequestHeader(value="f", required=true) String f,
                                                     @ApiParam(value = "g" ,required=true) @RequestHeader(value="g", required=true) String g) {
          getRequest().ifPresent(request -&gt; {
              for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                  if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                      ApiUtil.setExampleResponse(request, "application/json", "{  \"aNum\" : 0,  \"cNum\" : \"cNum\"}");
                      break;
                  }
              }
          });
          return new ResponseEntity&lt;&gt;(HttpStatus.NOT_IMPLEMENTED);
      }
  }
  </code></pre>
  
  <p>Following is the code snippet from my <code>GlobalExceptionHandler</code></p>
  
  <pre><code>class GlobalExceptionHandler extends ExceptionHandlerExceptionResolver implements HandlerExceptionResolver, Ordered, InitializingBean {
      ...
      @Override
      protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
          if (exception instanceof com.myframework.SystemException) {
              return new ServletInvocableHandlerMethod(this, exceptionMethods.get(com.myframework.SystemException.class.getName()));
          } else if (exception instanceof GenericApplicationException) {
              return new ServletInvocableHandlerMethod(this, exceptionMethods.get(com.myframework.GenericApplicationException.class.getName()));
          } else {
              return null;
          }
      }
      ....
  }
  </code></pre>