比方說,我有一個像倉庫:如何使用Spring Data Rest和PagingAndSortingRepository處理異常?
public interface MyRepository extends PagingAndSortingRepository<MyEntity, String> {
@Query("....")
Page<MyEntity> findByCustomField(@Param("customField") String customField, Pageable pageable);
}
這個偉大的工程。但是,如果客戶端發送一個形成的請求(比如說,在一個不存在的字段上搜索),那麼Spring會以JSON的形式返回異常。揭示了@Query
等
// This is OK
http://example.com/data-rest/search/findByCustomField?customField=ABC
// This is also OK because "secondField" is a valid column and is mapped via the Query
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField
// This throws an exception and sends the exception to the client
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah
拋出發送到客戶端異常的例子:
{
message:null,
cause: {
message: 'org.hibernate.QueryException: could not resolve property: blahblah...'
}
}
我如何處理這些例外?通常情況下,我爲我的MVC控制器使用@ExceptionHandler
,但我沒有在Data Rest API和客戶端之間使用一層。我是不是該?
謝謝。
鏈接到教程丟失 –