【问题标题】:Filter Results Using Query Parameter使用查询参数过滤结果
【发布时间】:2021-11-21 20:25:06
【问题描述】:

我正在尝试将 @Param 注释用于我使用 Spring Data JPA 的 Query Creation 构建的自定义查询,这样当我传递查询参数 ?organizationId=2 时,只会返回组织 ID 匹配的任务给定的参数。

@RepositoryRestResource(collectionResourceRel = "tasks", path = "tasks")
public interface TaskRepository extends JpaRepository<Task, Long> {

    List<Task> findByOrganizationId(@Param("organizationId") Long organizationId);
}

问题是当我访问/tasks?organizationId=2处的@RepositoryRestResource路径时,似乎调用了/tasks处暴露的默认List&lt;Task&gt; findAll();方法,所有Tasks都返回了。

如何让 Spring 将请求定向到我的自定义方法?

【问题讨论】:

    标签: spring-boot spring-data-jpa spring-data-rest


    【解决方案1】:

    所有查询方法资源都暴露在搜索资源下。 (有关详细信息,请参阅here。)

    http://localhost:8080/tasks/search/ 应该为您提供可用搜索端点的列表。一个应该是:http://localhost:8080/tasks/search/findByOrganizationId,您可以对其应用参数化搜索。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2012-04-20
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多