26

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
anup kumar agarwal
  • 263
  • 1
  • 3
  • 6

2 Answers2

43

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;
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
Laercio Metzner
  • 1,351
  • 11
  • 22
4

If you have an URL like : http://localhost:4567/smartapp/getDataViewModelConfig/456 use the following code :

get("/smartapp/getDataViewModelConfig/:id","application/json", ((request, response) -> {
                response.type("application/json")
                return  request.params(":id");
            }), gson::toJson);
Z.ABC
  • 91
  • 3