Roman Goyenko
Roman Goyenko

Reputation: 7070

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.

Upvotes: 4

Views: 9878

Answers (3)

Ironluca
Ironluca

Reputation: 3762

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)

Upvotes: 1

amoljdv06
amoljdv06

Reputation: 2864

About your query,you can use below comma separated format as well:

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

Upvotes: 0

gregwhitaker
gregwhitaker

Reputation: 13410

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
}

Upvotes: 7

Related Questions