如何在Spark Java框架中使用get方法获取请求参数?

26

我刚开始接触sparkjava。我想使用spark java读取我的请求参数,但我无法找到正确的语法。请帮助我解决这个问题。以下是我的路由方法和客户端调用:

我的客户端请求网址: /smartapp/getDataViewModelConfig?collId=123'

路由方法:

get("smartapp/getDataViewModelConfig/:id", "application/json", (request, response)

        -> {
  String id = request.params(":id");
}

'id'字段在这里返回了null。有什么建议可以说明出了什么问题吗?

2个回答

43

如果你要处理像/smartapp/getDataViewModelConfig?collId=123这样的URL,你需要在你的实现中处理查询参数,如下所示:

get("smartapp/getDataViewModelConfig", "application/json", (request, response)->{
  String id = request.queryParams("collId");
  return "HI " + id;
}

4
如果您有一个URL,例如:http://localhost:4567/smartapp/getDataViewModelConfig/456,请使用以下代码:
get("/smartapp/getDataViewModelConfig/:id","application/json", ((request, response) -> {
                response.type("application/json")
                return  request.params(":id");
            }), gson::toJson);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接