I have a basic JPA Entity that I would like to expose via Spring-Data-Rest
@Entity
@Table(name='test')
class Test{
@Id
Long id
@Column(name='other_id', unique = true)
String otherId
@Column(name='other_crm_id')
String otherCrmId
@Column(name='created_date')
Date createdDate
@Column(name='created_by')
String createdBy
}
I would like to change the exception message that is thrown when the Unique constraint on 'otherId' fires. Currently the default ExceptionHandler for Spring Data Rest displays the following
{
-cause: {
-cause: {
cause: null
message: "Unique index or primary key violation:
\"UK_PJCWVB8DO3C89YTD1PNF85HQR_INDEX_2 ON PUBLIC.TEST(OTHER_ID) VALUES
( /* key:1 */ 2, NULL, NULL, 'ABC123424', '123')\"; SQL statement:\
insert into test (id, created_by, created_date, other_crm_id,
other_id) values (null, ?, ?, ?, ?) [23505-175]"
}
message: "could not execute statement"
}
message: "could not execute statement; SQL [n/a]; constraint
[\"UK_PJCWVB8DO3C89YTD1PNF85HQR_INDEX_2 ON PUBLIC.TEST(OTHER_ID) VALUES ( /* key:1
*/ 2, NULL, NULL, 'ABC123424', '123')\"; SQL statement:\ insert into test (id,
created_by, created_date, other_crm_id, other_id) values (null, ?, ?, ?, ?)
[23505-175]]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
Not a big fan of the sql and table information being returned in the Response, so I've been attempting to change the message. I created a ExceptionHandlingController annotated with @ControlAdvice, but the ExceptionHandlers in AbstractRepositoryRestController.java take precedence.
My question is: What is the best way to change the error response for the DataIntegrityViolationException in Spring-Data-Rests default RepositoryEntityController?