2

I have 2 objects, Foo and Bar (a Foo is @ManyToOne with Bar), and a very basic repository interface in Spring Boot 2.0, and a method:

   List<Foo> findByBarIn(@Param("bar") List<Bar> bar);

This gets mapped by Spring to an endpoint called /foos/search/findByBarIn

I can specify a single bar doing something like

GET http://host/foos/search/findByBarIn?bar=http://host/bars/33 (where 33 is the ID the of the Bar entity)

But, how can I specify multiple bars?

I've tried: (with no success)

GET http://host/foos/search/findByBarIn?bar=http://host/bars/33,http://host/bars/44

GET http://host/foos/search/findByBarIn?bar=http://host/bars/33&bar=http://host/bars/44

Vlad Dinulescu
  • 1,173
  • 1
  • 14
  • 24
  • how is it relevant to Foo ? and is a requirement to do so via GET ? – nabeel Jan 28 '18 at 19:18
  • Have you tried findByBarIn?bar=33&bar=44&bar=55? And change '@Param' to '@RequestParam'. Why don't you use Long instead of Bar (as you pass only id)? – Justinas Jakavonis Jan 28 '18 at 20:01
  • I've tried also findByBarIn?bar=33&bar=44, it says "Parameter value element [1] did not match expected type [com.xxx.Bar (n/a)]" Param is fine, as it works with only 1 bar; but I've tried also your suggestion and replaced with RequestParam and I've got this message: "message": "Unable to detect parameter names for query method com.xxx.FooRepository.findByBarIn! Use @Param or compile with -parameters on JDK 8." – Vlad Dinulescu Jan 28 '18 at 22:13
  • It seems that you are passing a list of strings to your method parameter 'bar' redefine as : List findByBarIn(@Param("bar") List bar); I'd also suggest that you follow Justas suggestion and only pass in the bar-ids. – Tom Drake Jan 29 '18 at 00:36

1 Answers1

0

I've found out how to do it:

So, this prototype didn't work:

List<Foo> findByBarIn(@Param("bar") List<Bar> bars);

but this does:

List<Foo> findByBarIn(@Param("bar") Bar... bars);

And then, I can specify multiple bars by:

GET http://host/foos/search/findByBarIn?bar=http://host/bars/33&bar=http://host/bars/44
Vlad Dinulescu
  • 1,173
  • 1
  • 14
  • 24