在Spring数据休息中自定义查询和排序查询

时间:2022-09-11 16:34:46

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:

我正在使用Spring数据休息,并尝试使用@Query自定义两个重载的findAll方法的查询。但是,当我尝试这个时,我收到此错误:

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!

java.lang.IllegalStateException:检测到不明确的搜索映射。公共抽象org.springframework.data.domain.Page courses.CourseRepository.findAll(org.springframework.data.domain.Pageable)和公共抽象java.lang.Iterable courses.CourseRepository.findAll(org.springframework.data.domain。排序)映射到/ findAll!调整配置以获得明确的路径!

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

调用这些方法时,URL不按惯例包含/ findAll。用于检索未排序课程(但使用分页)的URL是

http://localhost:8080/v1/courses

HTTP://本地主机:8080 / V1 /课程

and sorting is

和排序是

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

HTTP://本地主机:8080 / V1 /课程排序=冠军

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.

我还尝试使用相同的错误消息在Course实体上使用@NamedQuery。

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.

我发现findAll(Pageable pageable)方法具有Sorting'内置',但它不对结果进行排序。当我点击以下URL并尝试按标题排序时,结果显然没有按标题排序。但是,如果没有自定义@Query,结果将被排序。

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

HTTP://本地主机:8080 / V1 /课程排序=冠军

2 个解决方案

#1


3  

Try

尝试

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

HTTP://本地主机:8080 / V1 /课程排序=标题,ASC

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

HTTP://本地主机:8080 / V1 /课程排序=标题降序

Or if you are using older Spring version, then take a look at this doc:

或者,如果您使用较旧的Spring版本,请查看此文档:

http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html

http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html

#2


0  

I guess you are trying overloading findall method of spring data. This can be achieved only by CustomRepository. Here is the useful link for you to guide about creating CustomRepository.

我猜你正在尝试重载findall方法的spring数据。这只能通过CustomRepository实现。以下是指导创建CustomRepository的有用链接。

CustomRepository-You can defined a class with the custom methods for tweaking Springdata's method based on the need.

CustomRepository-您可以使用自定义方法定义一个类,以根据需要调整Springdata的方法。

Spring documentation link for the same

Spring文档链接相同

#1


3  

Try

尝试

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

HTTP://本地主机:8080 / V1 /课程排序=标题,ASC

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

HTTP://本地主机:8080 / V1 /课程排序=标题降序

Or if you are using older Spring version, then take a look at this doc:

或者,如果您使用较旧的Spring版本,请查看此文档:

http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html

http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html

#2


0  

I guess you are trying overloading findall method of spring data. This can be achieved only by CustomRepository. Here is the useful link for you to guide about creating CustomRepository.

我猜你正在尝试重载findall方法的spring数据。这只能通过CustomRepository实现。以下是指导创建CustomRepository的有用链接。

CustomRepository-You can defined a class with the custom methods for tweaking Springdata's method based on the need.

CustomRepository-您可以使用自定义方法定义一个类,以根据需要调整Springdata的方法。

Spring documentation link for the same

Spring文档链接相同