2017-03-28 32 views
0

我为我的API使用Spark java。我有以下网址使用查询映射的Spark路由解析

get("/template/:id") -> controller.getById(req, res).handle(req, res) 
get("/template/:name") -> controller.getByName(req, res).handle(req, res) 

现在我的方法GetByName方法将不会被调用的请求将始终将其映射到:ID电话。那么有没有一种方法可以在spark-java中处理这个问题。 QueryMap可能很有用,但我不知道如何使用它。

+0

解析内部getByIdName的PARAMS你能给更多的环境?你在哪里使用这个代码,什么是get()? –

+0

得到是http获取请求,我在我的SparkConfig类中使用它来将http请求映射到控制器 –

回答

2

两种可能的解决方案可以使用:

  1. 创建两个不同的途径:

get("/template/id/:id") -> controller.getById(req, res).handle(req, res); get("/template/name/:name") -> controller.getByName(req, res).handle(req,res);

  • 创建只有一个路由和使用参数:
  • get("/template") -> controller.getByIdName(req, res).handle(req, res);

    然后用你想要的PARAMS称呼它,例如: "/template?id=1234&name=Joe"和使用req.queryParams("id")req.queryParams("name")