@ ExceptionHandler ( ConstraintViolationException . class ) public ResponseEntity < ExceptionInfo > handleConstraintViolationException ( final HttpServletRequest request , final ConstraintViolationException ex ) { logRequest ( request , ex ) ; final ExceptionInfo response = new ExceptionInfo ( ) ; response . setMessage ( ex . getConstraintViolations ( ) . stream ( ) . map ( violation -> violation . getPropertyPath ( ) + MESSAGE_FORMATTER_SEPARATOR + violation . getMessage ( ) + "." ) . collect ( Collectors . joining ( MESSAGE_FORMATTER_SEPARATOR ) ) ) ; response . setExceptionClass ( ex . getClass ( ) . getName ( ) ) ; response . setErrorCode ( SpServerError . SP_REPO_CONSTRAINT_VIOLATION . getKey ( ) ) ; return new ResponseEntity <> ( response , HttpStatus . BAD_REQUEST ) ; }
Method for handling exception of type ConstraintViolationException which is thrown in case the request is rejected due to a constraint violation . Called by the Spring - Framework for exception handling .