1 year ago

#65245

test-img

User27854

Java: Jersey: How to handle multiple query parameters

I have a rest API

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

The implementation for the same is

@Path("list")
@GET
public List<Todo> getTodos(){
  return todoService.getTodos();
}

I need to write the implementation for the below API's

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}

Is it possible for a single path annotation to handle all the implementations?

My concern is todo.completion.status=completed is defaulted in the query itself.

I have even looked at the below solution it does not work for me for the above mentioned reason.

Java REST API Query annotation

Update

I have avoided using @QueryParam as I have good number of arguments to be handled and the list is likely to grow. The method signature will only grow in future and it will affect readability of the code.

Hence, I have chosen to use UriInfo.

@Path("list")
@GET
public List<Todo> getTodos(@Context UriInfo uriInfo){
  MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
  .
  .
  .
}

The rest Query that I am building writes a query to collect information from other services. The issue with this approach is we can any parameters, How do I restrict the user to use only of the above parameters?

java

rest

web-services

jersey

jax-rs

0 Answers

Your Answer

Accepted video resources