I am making a Spring Boot app with MySQL database attached to it.
I have already marked username as unique in the user-table, and the DB throws a MySQLIntegrityConstraintViolationException when i try to add a new user with an existing username.
Is there a way to make the ControllerAdvice handle this exception ? I tried making the handler like this :
@ExceptionHandler(MySQLIntegrityConstraintViolationException.class)
@ResponseStatus(HttpStatus.CONFLICT)
@ResponseBody
public MyBadInputResponse databaseIntegrityViolationDuplicate(MySQLIntegrityConstraintViolationException ex) {
MyBadInputResponse bir = new MyBadInputResponse("The request value already exists in the database",
ex.getLocalizedMessage());
logger.error(bir.toString());
return bir;
}
But it doesn't work.