How to get the request parameters using get in Spark Java framework?

anup kumar agarwal picture anup kumar agarwal · Mar 18, 2015 · Viewed 26.8k times · Source

I'm new to sparkjava. I want to read my request params using spark java but I'm not able to find the correct syntax. please help me out. Below is my route method and the client call to it:

my client request url: /smartapp/getDataViewModelConfig?collId=123'

Route Method:

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

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

The 'id' field is returning null here. Any suggestions as to what went wrong here?

Answer

Laercio Metzner picture Laercio Metzner · Mar 27, 2015

If you have to work with an URL like /smartapp/getDataViewModelConfig?collId=123 you have to deal with query parameters in your implementation, like the following:

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