次のようなリポジトリがあるとします。
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 とクライアントの間のレイヤーは使用していません。するべきか?
ありがとう。