比方说,我有一个像仓库:如何使用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和客户端之间使用一层。我是不是该?
谢谢。
链接到教程丢失 –