Jak mogę obsługiwać wyjątki za pomocą Spring Data Rest i PagingAndSortingRepository?

Powiedzmy, że mam repozytorium takie jak:

public interface MyRepository extends PagingAndSortingRepository<MyEntity, String> {

    @Query("....")
    Page<MyEntity> findByCustomField(@Param("customField") String customField, Pageable pageable);
}

To działa świetnie. Jeśli jednak klient wyśle ​​uformowane żądanie (powiedzmy, szukając na polu, które nie istnieje), to Spring zwraca wyjątek jako JSON. Ujawnienie@Queryitd.

// 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

Przykład wyjątku zgłoszonego i wysłanego do klienta:

{
    message:null,
    cause: {
        message: 'org.hibernate.QueryException: could not resolve property: blahblah...'
    }
}

Jak mogę poradzić sobie z tymi wyjątkami? Normalnie używam@ExceptionHandler dla moich kontrolerów MVC, ale nie używam warstwy między API odpoczynku danych a klientem. Czy powinienem?

Dzięki.

questionAnswers(2)

yourAnswerToTheQuestion