How can I grab all query parameters in Jersey JaxRS?

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?

Answers


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


The unparsed query part of the request URI can be obtained from the UriInfo object:

@GET
public Representation get(@Context UriInfo uriInfo) {
  String query = uriInfo.getRequestUri().getQuery();
  ...
}

Adding a bit more to the accepted answer. It is also possible to get all the query parameters in the following way without adding an additional parameter to the method which maybe useful when maintaining swagger documentation.

@Context
private UriInfo uriInfo;

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

ref


Need Your Help

twitter bootstrap 3 tab view fade does not work properly

css twitter-bootstrap twitter-bootstrap-3

I use twitter bootstrap 3 applying .fade to show tabs with fade. That's fine except for the first tab's content is not shown for the first time:

Getting rid of null/empty string values in a C# array

c# arrays null string

I have a program where an array gets its data using string.Split(char[] delimiter).