Skip to content
Advertisement

Java Jersery: How to handle multiple Queries Parameters for a REST URL with UriInfo

I have implemented a rest Query as shown below:

@Path("list")
@GET
public List<Todo> getTodos(@Context UriInfo uriInfo){
  MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
  List<String> parameterList = queryParameters.get(assignee.name);   //Output -> name1,name2 parameterList --  size -1
  String parameter = queryParameters.getFirst(assignee.name); //Output -> name1,name2
  .
  .
  .
}

How do I handle when multiple parameters

http://localhost:9090/hello-todo/api/v1/todo/list?assignee.name={name1,name2}

Here instead of two strings, I am getting it as one single string. How should I handle it, should I split the parameter String by comma (,).?

When

Currently it is able to handle these endpoints. The Rest URL’s are

http://localhost:9090/hello-todo/api/v1/todo/list

http://localhost:9090/hello-todo/api/v1/todo/list?status=CRITICAL

http://localhost:9090/hello-todo/api/v1/todo/list?status=MAJOR

http://localhost:9090/hello-todo/api/v1/todo/list?status={criticality}&todo.completion.status=completed

http://localhost:9090/hello-todo/api/v1/todo/list?status={criticality}&todo.completion.status=completed&todo.title={title}

http://localhost:9090/hello-todo/api/v1/todo/list?status={criticality}&todo.completion.status=completed&todo.title={title}&todo.startDate={startDate}

Advertisement

Answer

If you want queryParameters.get(assignee.name); to return a list, you can include the parameter more than once in the URL

http://localhost:9090/hello-todo/api/v1/todo/list?assignee.name=name1&assignee.name=name2

Or you can continue to have a single parameter (list?assignee.name=name1,name2) and split on ,, but you have to write the code to do that, and consider what to do when one of your names has a , character in it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement