2

We use Spring Data Rest to build simple API for our internal webapp. Our repositories as below:

public interface BaseRepository<T, ID extends Serializable> {
  List<T> findByIdIn(@Param("ids") Collection<ID> ids);
}

public interface FooRepository extends BaseRepository<Foo, String> {}
public interface BarRepository extends BaseRepository<Bar, Long> {}

Our client fetch data, passing ids parameters as below:

  1. GET http://example.com/api/foos/search/findByIdIn?ids=ABC,XYZ --> It works well for String ids.

  2. GET http://example.com/api/bars/search/findByIdIn?ids=1,2,3 --> Got exception for numeric ids.

We got the exception: Parameter value element[1] did not match expected type [java.lang.Long (n/a)]

What's wrong with above repositories? What is the correct way to pass numeric ids?

Trung
  • 1,012
  • 13
  • 26
  • You haven't added the API code, Numeric ids might be coming in string format, parse it to Long. – Kaustubh Khare Apr 12 '21 at 04:01
  • Does this answer your question? [Parameter value \[1\] did not match expected type \[java.lang.Boolean\]](https://stackoverflow.com/questions/15791139/parameter-value-1-did-not-match-expected-type-java-lang-boolean) – Kaustubh Khare Apr 12 '21 at 04:04

1 Answers1

0

You may use this library which lets you build advanced search queries: https://github.com/turkraft/spring-filter

You can then simply do:

?filter= ids in (1, 2, 3, 4)

It supports enums, dates, booleans, logical operations, comparisons, and even joins (nested fields).

torshid
  • 137
  • 1
  • 9