Is there a way to have a variable number of parameters in a SparkJava route (i.e. a route that works with /:var1/:var2/:var3 as well as /:var1/:var2/:var3/:par4 and so on)?
1 Answer
No.
But instead, you can use a variable number of queryParams, since these are defined dynamically only when calling the route. Example:
If you wanted to support routes:
/someRoute/:var1/:var2/:var3/someRoute/:var1/:var2/:var3/:par4,
replace them with only /someRoute, and in its handler use request.queryMap() to get a mapping of [queryMap <---> its value].
Then, when you call this route you can call it with a variable number of queryParams:
/someRoute?var1=abc&var2=def&var3=ghi/someRoute?var1=abc&var2=def&var3=ghi&var4=jkl
The result of request.queryMap() for the first one will contain only 3 key-value pairs, and the second one will contain 4.
1 Comment
Niall O'Brien
I'd agree with this reply. Also note that generally speaking, two vars of nesting would be the recommended maximum I'd use, three in extreme examples. For everything else, use query params.