Я использую spting boot с mysql, и я сделал совет контроллера, чтобы поймать DataIntegrityViolationException s, когда нарушается уникальный индекс.
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(HttpStatus.CONFLICT)
@ResponseBody
public MyBadInputResponse databaseIntegrityViolationDuplicate(DataIntegrityViolationException ex){
String message = ex.getLocalizedMessage();
MyBadInputResponse bir = new MyBadInputResponse("Database did not like one of the variables in the request body",
"Database already contains an entry with that " + message);
logger.error(bir.toString());
return bir;
}
Вот как я добавил ограничение в свою таблицу:
alter table user
add constraint first_name_and_last_name
unique index first_name_last_name_UNIQUE
(first_name, last_name)
;
Однако это сообщение, которое мой ControllerAdvice получает из исключения:
не может выполнить инструкцию;SQL [н / п];ограничение [ null ];вложенное исключение - org.hibernate.exception.ConstraintViolationException: не удалось выполнить инструкцию
Я хотел бы, чтобы сообщение содержало имя ограничения. Поэтому вместо:
ограничение [null];
должно быть:
ограничение [first_name_and_last_name];
Пожалуйста, помогите.