Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS query with multiple parameters of the same name

I want to make a web-service where you can query for generic data. So, my URL would be something like this:

.../field_name/Country/field_value/US/field_name/City/field_value/Boston

what would be the way to read it with Jersey - I want to get an array of field names and a corresponding array of field values.

like image 610
Roman Goyenko Avatar asked Nov 16 '25 10:11

Roman Goyenko


2 Answers

This would seem to be better suited for query parameters and not url parameters as you have above.

Your url would then look something like:

/query?Country=US&City=Boston&City=Chicago

JAX-RS supports multiple query parameters of the same name by mapping them to a Collection in your endpoint as follows:

@GET
@Path("/query")
public String queryValues(@QueryParam("Country") List<String> countries,
                          @QueryParam("City") List<String> cities) {
   // Do work here
}

If the query parameters are completely dynamic and you don't know what they are until runtime then you can inject the UriInfo object and interrogate the query string yourself.

@GET
@Path("/query")
public String queryValues(@Context UriInfo uriInfo) {
   // Do work here
}
like image 73
gregwhitaker Avatar answered Nov 18 '25 01:11

gregwhitaker


Adding an observation: For Jersey 2.35 FormParam, it needs to be City=Boston&City=Chicago style, which then can become elements of a list or set.

The comma separated parameter values (e.g City=Boston,Chicago) doesn't seem to work. It takes it as a single value (Tested with Postman)

like image 35
Ironluca Avatar answered Nov 18 '25 00:11

Ironluca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!