【问题标题】:Spring Data Rest: pass Collection<Entity> as query String parameterSpring Data Rest:将 Collection<Entity> 作为查询字符串参数传递
【发布时间】:2016-08-12 06:46:13
【问题描述】:

首先,这与 Spring Data Rest: How to search by another object's key? 有关,这似乎在 https://jira.spring.io/browse/DATAREST-502 中得到解决

我的问题是(我相信)和扩展。我看到以下行为:

  1. 我定义了两个存储库查询,例如

    Person findByAccount(@Param("account") Account account));
    
    Collection<Person> findByAccountIn(@Param("accounts") Collection<Account> accounts));
    
  2. 两种搜索方法都通过 spring-data-rest 公开。我可以使用http://localhost:8080/people/search/findByAccount?account=http://localhost:8080/accounts/1 等网址访问第一个
  3. 我可以使用http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1 之类的url 访问第二种方法,但是如果我尝试传入多个帐户,例如

    http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1,http://localhost:8080/accounts/2,
    

    它将运行查询,但忽略第一个帐户 (http://localhost:8080/accounts/1) 并仅基于第二个帐户 (http://localhost:8080/accounts/2) 进行搜索

通过 REST API 将实体集合传递给存储库参数的正确技术是什么?我发现它适用于单个实体,但不适用于集合。请注意,直接访问 JpaRepository 时,这两种存储库方法都按预期工作。

还要注意,如果集合是某种原始类型,这些查询似乎可以工作,例如,findByAccountIdIn(@Param("accountIds") Collection&lt;Long&gt; accountIds) 可通过http://localhost:8080/people/search/findByAccountIdIn?accountIds=1,2 使用预期功能访问。这让我相信,将 URI 列表传递给需要相应实体集合的查询方法的方式可能是错误的。

提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    尝试重复查询参数,因为大多数服务器会将其解释为列表。它可能不是最漂亮的解决方案,但应该可以。

    http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1&accounts=http://localhost:8080/accounts/2
    

    【讨论】:

    • 感谢您的及时回复,很遗憾您的建议不起作用。看来 Spring Data Rest 不支持这一点(或者至少我没有正确调用它)。例如,以下内容:http://localhost:8080/people/search/findByAccountIdIn?accountIds=1,2 按预期工作,但http://localhost:8080/people/search/findByAccountIdIn?accountIds=1&amp;accountIds=2 产生错误页面(正如您提到的建议一样)
    • 嗯,是的,似乎 sprint data-rest 不像大多数服务器。我一直试图让它与我自己的一个小型弹簧数据休息解决方案一起工作,但到目前为止无济于事。我认为您可能需要创建 rest 存储库的自定义实现并根据需要实现端点。看看这个创建自定义方法的教程javabeat.net/spring-data-custom-repository我会继续努力,如果有什么我会告诉你的。
    • 感谢您对我的问题的投资:)。也许这是一个值得在 SO 之外报告的问题 - 如果 Collection 的功能存在,它也应该适用于 Collection
    • 感谢该链接中的建议 - 我注意到一个问题是自定义存储库方法不会通过 spring-data-rest 自动公开,因此还有另一个过程可以单独公开该方法restController 并将其添加为 /search 资源下的链接,如果在多个地方完成可能会有点费力。我认为与此同时,我将使用采用 Collection IDs 的存储库方法,希望将来可以解决这个问题——据我所知,在 REST API 中,ID 应该是 URL
    • 抱歉,我正在度假,回复晚了。是的,如果您想在多个地方进行操作,可能会很费力。您的权利也许值得在 Spring 论坛之一中提及。祝你的解决方案好运!
    【解决方案2】:

    我知道这已经过时了,但我找到了答案。
    我会把这个放在这里给任何应该这样徘徊的人:

    How to use List in endpoint exported by Spring Data REST?

    List&lt;Person&gt; findByAccountIn(@Param("accounts") Account... accounts);

    请求如下所示:

    http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1&accounts=http://localhost:8080/accounts/2&accounts=http://localhost/accounts/anotheraccountid

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      相关资源
      最近更新 更多