更改DataIntegrityViolationException的Spring Data Rest默认消息

时间:2014-09-29 16:01:14

标签: spring-data-rest

我有一个基本的JPA实体,我想通过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
}

我想更改当'otherId'上的Unique约束触发时抛出的异常消息。目前,Spring Data Rest的默认ExceptionHandler显示以下内容

{
-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"
}

不是响应中返回的sql和表信息的忠实粉丝,所以我一直在尝试更改消息。我创建了一个使用@ControlAdvice注释的ExceptionHandlingController,但AbstractRepositoryRestController.java中的ExceptionHandlers优先。

我的问题是:在Spring-Data-Rests默认的RepositoryEntityController中更改DataIntegrityViolationException的错误响应的最佳方法是什么?

0 个答案:

没有答案