Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 491
Tom Avatar asked Apr 19 '11 15:04

Tom


People also ask

How do you pass multiple query parameters in restful web services?

@PUT @Path("{user}/{directory:. +}") public Response doshare(@PathParam("user")String name, @PathParam("directory")String dir, @QueryParam("name")String sharename, @QueryParam("type")String type){ mongoDAOImpl impl=new mongoDAOImpl(); Mongo mongo=impl. getConnection("127.0. 0.1","27017"); DB db=impl.


2 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

like image 128
hisdrewness Avatar answered Sep 17 '22 17:09

hisdrewness


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();   ... } 
like image 28
glerup Avatar answered Sep 20 '22 17:09

glerup