hercuptruk
hercuptruk

Reputation: 93

Spring Data Rest: pass Collection<Entity> as query String parameter

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!

Upvotes: 2

Views: 3036

Answers (2)

Airborne Eagle
Airborne Eagle

Reputation: 453

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

Upvotes: 0

Schokea
Schokea

Reputation: 708

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

Upvotes: 1

Related Questions