1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SuperCow
  • 1,523
  • 7
  • 20
  • 32
  • Query parameters don't normally work like that. You typically define a set list of parameters and build your query off of that in a familiar pattern-like fashion. What are you actually attempting to solve with an approach like this? What is its use case? – Makoto Dec 19 '17 at 22:18
  • Which REST implementation are you using? – 11thdimension Dec 19 '17 at 22:25
  • @Makoto added more info – SuperCow Dec 19 '17 at 23:11
  • Thanks for that. So...you're not going to like this comment, but your work is well superseded by other frameworks, like [Rest Assured](http://rest-assured.io), which give you the ability to do things like this. You can also just use Mockito to mock out a response if you *really* want to do that. – Makoto Dec 19 '17 at 23:33
  • The [Wiremock](http://wiremock.org/) project should be preferable. – tkruse Dec 20 '17 at 01:05
  • Possible duplicate of [Spring MVC - How to get all request params in a map in Spring controller?](https://stackoverflow.com/questions/7312436/spring-mvc-how-to-get-all-request-params-in-a-map-in-spring-controller) – tkruse Dec 20 '17 at 01:06
  • @Makoto Yeah, the idea here is to allow QA/testers to create their own mock responses (json or xml) without having to make any modifications to the codebase that they are testing -- then when we have a real url we can just change the path in a config file to stop using the mock application. I haven't used Rest Assured before, I'll have to check that out and see what functionality it has. Also we like to use the mock service because it tests for things like if our application can connect to another actual application, security certificates are working, etc. – SuperCow Dec 21 '17 at 19:00

1 Answers1

0

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";
}
tkruse
  • 10,222
  • 7
  • 53
  • 80