Can a query parameter name be variable? Like: @QueryParam("//anything")

1.5k Views Asked by At

I have a resource class and within it a @GET that takes one query param called operation (this should be static) and then I want to take a variable number of other query params that can be named anything.

My first thought was to do something like this:

public Response get(
    @QueryParam("operation") String operation, 
    @QueryParam("list") final List<String> list) {
    //do stuff
}

The problem here is that I would have to make a request like:

...?operation=logging&list=ABC&list=XYZ

While what I want is to be able to have something like this:

...?operation=logging&anything=ABC&something_else=XYZ

Is there a way to make the list query param @QueryParam(//anything)?

In doing some information gathering I ran across this sort of approach:

@GET
public void gettest(@Context UriInfo ui) {
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();   
    String operation = queryParams.getFirst("operation");   
    for (String theKey : queryParams.keySet()) {
        System.out.println(queryParams.getFirst(theKey));
        //do stuff with each other query param
    }   
}

Is multivaluedmap the way to go for this situation -- Or is there a way to use a variable query param name? Or a better approach? Thanks.

Edit/Update:

This is using javax.ws.rs

The use case is: this application being used as a tool for mocking responses (used for testing purposes in other applications). The mock responses are retrieved from a DB by looking up the 'operation' and then some sort of 'id'. The actual id used could be any of the "list" query params given. The reason for this is to give flexibility in different applications to use this mock service -- the urls in applications may be constructed many different ways and this makes if so one doesn't have to change around their code to be able to use the mock service.

1

There are 1 best solutions below

0
On

As in this question, use a map:

@RequestMapping(value = {"/search/", "/search"}, method = RequestMethod.GET)
public String search(
@RequestParam Map<String,String> allRequestParams, ModelMap model) {
   return "viewName";
}