Play framework 2: Use Array[String] in route
Asked Answered
B

4

14

I want to generate an url like this:

/photo?tags=tag1,tag2,tag3

routes file:

GET     /photo  controllers.Photos.list(tags:Array[String] ?= "")

I got this errors in play console:

No QueryString binder found for type Array[String]

What is the best way to achieve this ?

Thanks.

Boggs answered 2/7, 2012 at 9:38 Comment(1)
correct way of doing an array in query params(no matter the framework) from what I understand is actually like this /photo?tags=tag1&tags=tag2&tags=tag3 ...it's a bit verbose but the specification allows repetition like thatFat
C
9

I think that you should use a common String and then take care about converting it to an Array in your controller

routes:

GET     /photo  controllers.Photos.list(tags:String ?= "")

in Java:

public static Result list (String tags){
    String[] tagsArray = tags.split(",");
    // do something with tagsArray
    return ok();
}
Conclave answered 2/7, 2012 at 9:44 Comment(2)
That was my first idea. I will use this solution.Boggs
The way described by @Ahmed Aswani below is the "correct" way to do this in HTTP.Higginson
D
23

play will bind to array's/lists when the values are in the query string or post data with the same name.

this also seems to work:

This route: http://localhost/controller/{id} 

This url: http://localhost/controller/1?id=2&id=3

Will bind to controller(int[] id) where id -> {1, 2, 3}

posting id=2&id=3 will also bind to an array.

reference: https://groups.google.com/forum/?fromgroups#!topic/play-framework/c5kB6wmcF8Q

Doubleness answered 2/7, 2012 at 14:13 Comment(0)
F
11

Using a list instead of an array should work.

If you are using Java, it works like this:

GET     /photo  controllers.Photos.list(tags: java.util.List[String])
Fenland answered 15/11, 2014 at 20:21 Comment(1)
I've searched for this for several hours! It turns out you cannot just use List[String]. it will use scala type by default! The following exception will appear! type mismatch; found : List[String] (in scala.collection.immutable) required: List[_] (in java.util) Use the full package name is the correct way to goDepute
C
9

I think that you should use a common String and then take care about converting it to an Array in your controller

routes:

GET     /photo  controllers.Photos.list(tags:String ?= "")

in Java:

public static Result list (String tags){
    String[] tagsArray = tags.split(",");
    // do something with tagsArray
    return ok();
}
Conclave answered 2/7, 2012 at 9:44 Comment(2)
That was my first idea. I will use this solution.Boggs
The way described by @Ahmed Aswani below is the "correct" way to do this in HTTP.Higginson
I
4

As an aside, if you wish to pass an array of Longs, this works:

GET /photo controllers.Photos.list(tags: java.util.List[java.lang.Long])

with the controller function taking List<Long> tags as an argument.

Isodynamic answered 5/1, 2017 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.