假设我有一个存储库,例如:
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...'
}
}
我该如何处理这些异常?通常,我将@ExceptionHandler
用于我的 MVC 控制器,但我没有使用 Data Rest API 和客户端之间的层。我是不是该?
谢谢。