Named path and query parameters

21 views
Skip to first unread message

Owen Convey

unread,
Mar 15, 2017, 10:06:42 AM3/15/17
to sparkjava
Hey folks,

First off I'd like to say how awesome Spark is!  I've just discovered it and found it to be a real breath of fresh air and it's lots of fun to develop in!

I had a question about parameters and didn't find anything on it in the documentation nor via google.  Given a route like this (I used a statement lambda to make it clearer):

get("location/:id", (request, response) -> {
    String id = request.params("id");
    return JSON.toJson(findLocationTrack(id).orElseThrow(notFoundLocation(id)));
});


Is it possible to bind the path or query variables so they are automatically cast to a type and available for use, something like (pseudo code):

get("location/:id", String id, (id,request, response) -> {
      return JSON.toJson(findLocationTrack(id).orElseThrow(notFoundLocation(id)));
});


Hopefully my question is clear enough and thanks for the great framework!

domin...@gmail.com

unread,
Mar 16, 2017, 3:54:32 AM3/16/17
to sparkjava
There isn't an overloaded handle method within Request interface that allows this type of 'binding'

I don't think it would be a good idea either. Request has many methods that have parameterised look ups like request.params(key).  Docs#Request You'd have trouble making this work clearly. And then there are `Splat` methods to cover as well.
Reply all
Reply to author
Forward
0 new messages