Routing in Java Play Framework 2, List<> as par

2019-05-29 05:41发布

问题:

I'm trying to send a List of Infoobjects to my controller. I need to specify the routes file. I know how to send int,string and long as parameters, but how about a List<>?

I have tried with this, but it's not working and i'll get an error message saying "not found: type Infoobject".

GET    /generateExcel                  controllers.Application.generateExcel(list:List[Infoobject])

Thanks!

回答1:

First of all, I think you need to put the package of your object in the [] of the list : List[path.of.your.package.Infoobject] (there are no imports in your routes).

Anyway, I think you can only put String and Numerals in the URL, in order to send arguments to your controllers.

1rst workaround : You can create a QueryStringBindable that binds your object (List[Infoobject]) to a string, so that the information sent via URL as a String is recovered by your controller as a List[Infoobject].

2nd workaround : Your can send Strings that could help you recreate the List in you controller (If you query the database for example, or if it's a List of object that only contains String attributes)

Here is the java doc for the QueryStringBindable : http://www.playframework.com/documentation/api/2.1.1/java/play/mvc/QueryStringBindable.html



回答2:

Routes aren't best place for passing complicated objects, instead of passing a List (as other pointed yet in answers/comments) you would do better by sending a comma separated list of id's of the objects persisted in the database. Remember, that in many places you can hit the wall ie. limit of the query's length or something.

If you can't/don't want to persist them in DB you can also just use a Cache API for storing whole list in the memory between the requests, so you can send in the url the ID of the cache entry as a common String.