12

次のようなリポジトリがあるとします。

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...'
    }
}

これらの例外を処理するにはどうすればよいですか? 通常、@ExceptionHandlerMVC コントローラーには を使用しますが、Data Rest API とクライアントの間のレイヤーは使用していません。するべきか?

ありがとう。

4

2 に答える 2

1

@ControllerAdviceを使用して、コンテンツを思いのままにレンダリングできます。の作業方法を知る必要がある場合は、ここにチュートリアルがあります。ControllerAdvice戻ることを忘れないでくださいHttpEntity

于 2014-01-27T21:44:31.590 に答える