'ConstraintViolationException is not called by ResponseEntityExceptionHandler

I have a Spring Boot RESTful API and I am creating the error system for my API. I want to be able to catch each errors and each validation exceptions in my class that extend ResponseEntityExceptionHandler.

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
    HttpHeaders defaultHttpHeaders = new HttpHeaders();

    public RestResponseEntityExceptionHandler() {
        this.defaultHttpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
    }

    @ExceptionHandler({ ConstraintViolationException.class })
    protected ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex, WebRequest request) {
        logger.info("CONSTRAINT VIOLATION EXCEPTION");
        List<ValidationErrorResponse.ValidationError> errors = new ArrayList<>();
        List<String> tests = new ArrayList<>();

        for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
            tests.add(violation.getRootBeanClass().getName() + " " +
                    violation.getPropertyPath() + ": " + violation.getMessage());
        }

        return new ResponseEntity<Object>(tests, new HttpHeaders(), HttpStatus.MULTI_STATUS);
    }

    @ExceptionHandler(value = {IncidentologistNotFoundException.class, UserNotFoundException.class, ReturnRequestNotFoundException.class})
    protected ResponseEntity<Object> handleEntityNotFoundException(RuntimeException ex, WebRequest request) {
        ErrorResponse errorResponse = new ErrorResponse(
                ErrorTypes.ENTITY_NOT_FOUND,
                HttpStatus.NOT_FOUND,
                "The entity has not been found",
                ex.getMessage()
        );
        return handleExceptionInternal(ex, errorResponse, new HttpHeaders(), errorResponse.getStatus(), request);
    }
}

The handleEntityNotFoundException() works as excepted.
However, handleConstraintViolation() which is supposed to catch all the exceptions related to the ConstraintViolation is not called at all. I do not know why, because, I marked my @RequestBody with the @Valid annotation in my controller:

    @PostMapping("/return-requests")
    ResponseEntity<?> postReturnRequest(@Valid @RequestBody MutateReturnRequestDto newReturnRequest) {
        Incidentologist requester = incidentologistRepository.findById(newReturnRequest.getIcRequesterId())
                .orElseThrow(() -> new IncidentologistNotFoundException(newReturnRequest.getIcRequesterId()));

        ReturnRequest returnRequest = manuallyConvertToEntity(newReturnRequest);
        returnRequest.setIcRequester(requester);

        ReturnRequest createdReturnRequest = returnRequestService.createReturnRequest(returnRequest);

        return new ResponseEntity<>(convertToDto(createdReturnRequest), HttpStatus.CREATED);
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source