2

First off, this is related to Spring Data Rest: How to search by another object's key? which appears to be resolved in https://jira.spring.io/browse/DATAREST-502

My issue is (I believe) and extension of this. I'm seeing the following behavior:

  1. I have two repository queries defined e.g.

    Person findByAccount(@Param("account") Account account));
    
    Collection<Person> findByAccountIn(@Param("accounts") Collection<Account> accounts));
    
  2. Both search methods are exposed via spring-data-rest. I can access the first using a url such as http://localhost:8080/people/search/findByAccount?account=http://localhost:8080/accounts/1
  3. I can access the second method using a url such as http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1, but if I try to pass in MULTIPLE accounts, such as

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

    It will run the query except will IGNORE the first account (http://localhost:8080/accounts/1) and only search based on the second (http://localhost:8080/accounts/2)

What is the proper technique for passing in a Collection of entities to a repository argument over the REST API? I find it works great for a single entity, but not for a Collection. Note that both of these repository methods are working as expected when directly accessing the JpaRepository.

Also note that these queries seem to work if the collection is of some primitive type, for example findByAccountIdIn(@Param("accountIds") Collection<Long> accountIds) is accessible with intended functionality via http://localhost:8080/people/search/findByAccountIdIn?accountIds=1,2. This leads me to believe it's possibly an error in the way a list of URIs is passed into a query method which expects a Collection of corresponding entities.

Thanks in advance for any help!

Community
  • 1
  • 1
hercuptruk
  • 83
  • 2
  • 4

2 Answers2

1

Try repeat the query parameter as most servers will interpret this as a list. It might not be the prettiest solution but should work.

http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1&accounts=http://localhost:8080/accounts/2
Schokea
  • 708
  • 1
  • 9
  • 28
  • Thanks for the prompt reply, unfortunately what you suggested isn't working. It appears Spring Data Rest is not supporting that (or at least im not invoking it properly). For example, the following: `http://localhost:8080/people/search/findByAccountIdIn?accountIds=1,2` works as expected, but `http://localhost:8080/people/search/findByAccountIdIn?accountIds=1&accountIds=2` produces an Error page (as does the suggestion you mentioned) – hercuptruk Apr 19 '16 at 22:47
  • hmmm yes it seems sprint data-rest isn't like most servers. I've been trying to get it working also with a small spring data-rest solution of my own but to no avail so far. I think you might need to create a custom implementation of the rest repository and implement the endpoint as you wish. Take a look at this tutorial for creating custom methods http://www.javabeat.net/spring-data-custom-repository/ I'll keep trying if I get anything I'll let you know. – Schokea Apr 20 '16 at 10:58
  • Thanks for your investment in my question :). Perhaps it's an issue worth reporting outside of SO - it seems if the functionality is there for Collection, it should also work for Collection – hercuptruk Apr 20 '16 at 19:08
  • Thanks for the suggestion in that link - I notice one problem is that the custom repository methods aren't automatically exposed via spring-data-rest, so there is another process to expose that method on a separate restController and add it as a link under the /search resource which can be kind of laborious if done in multiple places. I think in the meantime I'll use repository methods which take Collection IDs, with the hope that this may be resolved in the future - as I understand that in a REST API the ID should be a URL – hercuptruk Apr 20 '16 at 19:17
  • Sorry for the late response I was on vacation. Yes it can be laborious if you want to do it in multiple places. Your right maybe worth mentioning in one of the Spring forums. Good luck with your solution! – Schokea Apr 26 '16 at 10:42
0

I know that this is forever old, but I found the answer.
I will put this here for anyone else who should wander this way:

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

List<Person> findByAccountIn(@Param("accounts") Account... accounts);

the request would look like this:

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

Airborne Eagle
  • 373
  • 5
  • 11