1

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
  • ...the doc doesnt let assume so ... , but "Routes.Path Groups" seem suitable for your need (!?) Commented May 6, 2018 at 12:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.