Matthias Merkel
Matthias Merkel

Reputation: 111

SparkJava variable number of parameters

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)?

Upvotes: 1

Views: 935

Answers (1)

SHG
SHG

Reputation: 2616

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.

Upvotes: 2

Related Questions