In JAX-RS, I can define a query parameter to populate a list:
@GET
@Path("/foo")
public String myService(
@DefaultValue("default") @QueryParam("p") List<String> items
) {
return items.toString();
}
For a request like .../foo?p=1&p=2
, items is ["1","2"]
.
@DefaultValue
sets a default, but this always creates a collection with a single entry containing that default: for a request like .../foo
, items is ["default"]
.
I would like a default which contains two entries. A fantasy approach that doesn't work is:
@DefaultValue("foo","bar") List<String> items
The working code I have instead omits DefaultValue and instead has:
if(items.isEmpty()) {
items = asList("foo","bar");
}
Is there a clean JAX-RS way to achieve the same thing?
Using asList() allows you to populate an array with a list of default values. This can be more efficient than using multiple add() statements to add a set of default values to an ArrayList.
Might be too simplistic but a possible way to pass multiple default values for a list could be as follows:
public void dummy(@DefaultValue("foo,bar") String items) {
List<String> parameters = Lists.newArrayList(Splitter.on(",").split(items));
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With