Customize query for paging and sorting in Spring data rest

Viewed 5872

I am using Spring data rest and am trying to customize the query for two of the overloaded findAll methods using @Query. However, when I attempt this I receive this error:

java.lang.IllegalStateException: Ambiguous search mapping detected. Both public abstract org.springframework.data.domain.Page courses.CourseRepository.findAll(org.springframework.data.domain.Pageable) and public abstract java.lang.Iterable courses.CourseRepository.findAll(org.springframework.data.domain.Sort) are mapped to /findAll! Tweak configuration to get to unambiguous paths!

When these methods are called the URL does not contain /findAll by convention. A URL for retrieving unsorted courses (but using paging) is

http://localhost:8080/v1/courses

and sorting is

http://localhost:8080/v1/courses?sort=title

Here's the relevant code, which is fairly straightforward:

public interface CourseRepository extends PagingAndSortingRepository<Course, Long> {

  @Override
  @Query("SELECT c FROM Course c WHERE c.visible = 'Yes'")
  Page<Course> findAll(Pageable pageable);

  @Override
  @Query("SELECT c FROM Course c WHERE c.visible = 'Yes'")
  Iterable<Course> findAll(Sort sort);
}

I've also attempted using an @NamedQuery on the Course entity with the same error message.

EDIT:

I have discovered that the findAll(Pageable pageable) method has Sorting 'built-in', however it does not sort the results. When I hit the following URL and attempt to sort by title the results are clearly not sorted by title. However, without the custom @Query the results are sorted.

http://localhost:8080/v1/courses?sort=title

2 Answers
Related