title: Spring Boot @ControllerAdvice / @Valid
body: <p>I am working on sample demo application for Exception Handling in Spring Boot.I am trying Exception Handling With @ControllerAdvice.</p>
  
  <p>I would like to handle exception thrown by validator. It handles other exceptions but not MethodArgumentNotValidException.</p>
  
  <p>For more details following are the classes I am working on:</p>
  
  <p><strong>Query.java</strong></p>
  
  <pre><code>@Getter
  @Setter
  @NoArgsConstructor
  @Validated
  public class Query implements Serializable{
      @Size(min = 7, max = 24, message = "Size must be between 7 and 24")
      @Pattern(regexp = "[a-zA-Z0-9 ]+", Invalid characters")
      private String number;
  
      @Size(max = 2, message = "Size must be between 0 and 2")
      @Pattern(regexp = "[a-zA-Z0-9 ]+", message="Invalid characters")
      private String language;
  }
  </code></pre>
  
  <p><strong>ErrorResponse.java</strong></p>
  
  <pre><code>@Setter
  @Getter
  @NoArgsConstructor
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @Data
  public class ErrorResponse 
  {
  
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
      private LocalDateTime timestamp;
  
      private HttpStatus status;
  
      private int code;
  
      private String error;
  
      private String exception;
  
      private String message;
  
      private String path;
  
      private List&lt;String&gt; errors;
  
  }
  </code></pre>
  
  <p><em>CustomExceptionHandler.java</em></p>
  
  <pre><code>@SuppressWarnings({"unchecked","rawtypes"})
  @ControllerAdvice
  @Component("error")
  public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
  
      @ResponseStatus(HttpStatus.NOT_FOUND)
      @ExceptionHandler(NotFoundException.class)
      public final ResponseEntity&lt;Object&gt; handleNotFoundError(NotFoundException ex, final HttpServletRequest request) {
          ErrorResponse error = new ErrorResponse();
          error.setTimestamp(LocalDateTime.now());
          error.setMessage(ex.getMessage());
          error.setCode(HttpStatus.NOT_FOUND.value());
          return new ResponseEntity(error, HttpStatus.NOT_FOUND);
      }
  
      @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
      @ExceptionHandler(InternalServerException.class)
      public final ResponseEntity&lt;Object&gt; handleInternelServorError(InternalServerException ex, final HttpServletRequest request) {
          ErrorResponse error = new ErrorResponse();
          error.setTimestamp(LocalDateTime.now());
          error.setMessage(ex.getMessage());
          error.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
          return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
      }
  
      @ExceptionHandler(ConstraintViolationException.class)
      public void constraintViolationException(HttpServletResponse response) throws IOException {
          response.sendError(HttpStatus.BAD_REQUEST.value());
      }
  
      @ResponseStatus(HttpStatus.BAD_REQUEST)
      @Override
      protected ResponseEntity&lt;Object&gt; handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
          List&lt;String&gt; errorList = ex
                  .getBindingResult()
                  .getFieldErrors()
                  .stream()
                  .map(fieldError -&gt; fieldError.getDefaultMessage())
                  .collect(Collectors.toList());
          ErrorResponse error = new ErrorResponse();
          error.setCode(HttpStatus.BAD_REQUEST.value());
          return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
      }
  }
  </code></pre>
  
  <p><strong>Request</strong></p>
  
  <pre><code>public ResponseEntity&lt;?&gt; getData(HttpServletRequest httpServletRequest,
              @Valid @ApiParam(value = "MANDATORY. The number") @PathVariable(value = "number", required = true) final String partNumber,
              @Valid @ApiParam(value = "OPTIONAL. The language") @RequestParam(value = "language", required = false) final String languageKey) {
  .............
  }
  </code></pre>