multiple path params in Sparkjava rest api

629 views
Skip to first unread message

Prash

unread,
Mar 24, 2017, 1:52:05 PM3/24/17
to sparkjava
Hi,

My rest api demands me to have multiple path params; for e.g.

/logs/<record_type>/<record_id>

Here I have two path params , "record_type" and "record_id".

When I hit with the url http://localhost:4567/logs/abc/23, (here "abc" is record_type and "123" is record_id) it should invoke the particular route method but instead says , "route not configured".

If I have a single path param , then it works and get routed to proper method but multiple path param , it gets lost finding the route.

Your quick help is appreciated.

Regards,
Prashant

Here is my code;

get("/logs/:record_type/:record_id", (req, res) -> {
req.
String recordType = req.params(":record_type");
String recordId = req.params(":record_id");
AuditLogModel requestModel = new AuditLogModel();
requestModel.setRecordType(recordType);
requestModel.setRecordId(recordId);
return auditService.getAuditLog(requestModel);
}, json());

domin...@gmail.com

unread,
Mar 24, 2017, 7:11:01 PM3/24/17
to sparkjava
Perhaps you can use Splat Route.

// matches "GET /say/hello/to/world"
// request.splat()[0] is 'hello' and request.splat()[1] 'world'
get("/say/*/to/*", (request, response) -> {
    return "Number of splat parameters: " + request.splat().length;
});
Reply all
Reply to author
Forward
0 new messages