How can I grab all query parameters in Jersey JaxRS?

Tom picture Tom · Apr 19, 2011 · Viewed 60.9k times · Source

I am building a generic web service and need to grab all the query parameters into one string for later parsing. How can I do this?

Answer

hisdrewness picture hisdrewness · Apr 19, 2011

You can access a single param via @QueryParam("name") or all of the params via the context:

@POST
public Response postSomething(@QueryParam("name") String name, @Context UriInfo uriInfo, String content) {
     MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
     String nameParam = queryParams.getFirst("name");
}

The key is the @Context jax-rs annotation, which can be used to access:

UriInfo, Request, HttpHeaders, SecurityContext, Providers