Kris
Kris

Reputation: 5792

How to route map-like parameters in GET request with Play

I have to handle the following GET request in my Play application eg.

http://host/somemethod?queries[search]=asdf&perPage=100&sorts[name]=0 

Now, normally GET requests like:

http://host/somemethod?queries=asdf&perPage=100&sorts=0

can be easily handled with eg.

GET         /somemethod     controllers.SomeMethodController.getPeople(queries:String ?= "", perPage: Int ?= 10, sorts:String ?= "")

but the params like: queries[search]=asdf or sorts[name]=0 are not resolved properly (tried simple String type, Seq[String] or Map[String,String] - the last one throws exception about missing QueryStringBinder)

How do I map those query params to a method parameters with Play routing? Do I need to create my own custom QueryStringBinder?

Upvotes: 3

Views: 1515

Answers (1)

Alexander B
Alexander B

Reputation: 1033

Your assumption is correct. Maps are not supported by the default QueryString binder and you therefor should implement your own QueryStringBindable. The Javadoc of QueryStringBindable should give you a hint how to implement it.

Play 2.x supports the following query string parameter types:

  • String
  • Char
  • Int
  • Integer (Java)
  • Long (Scala and Java)
  • Double (Scala and Java)
  • Float (Scala and Java)
  • Boolean (Scala and Java)
  • java.util.UUID
  • Option
  • Optional (Java)
  • Seq
  • List (Scala and Java)
  • Array

See Binders.scala for all supported types.

Upvotes: 1

Related Questions