1

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?

Beanz
  • 208
  • 3
  • 12
  • I think SDR lacks customizing exception message, unless there are improvements in newer versions – Stackee007 Sep 29 '14 at 16:10
  • I haven't been able to find a way to hook into the existing controller. I may just make a base @RestController for that one entity. – Beanz Sep 29 '14 at 18:06
  • I ended up just creating a controller with the one RequestMapping for the POST request, and let the default controllers handle the rest. – Beanz Sep 29 '14 at 19:49

0 Answers0