Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@DefaultValue for a list

Tags:

java

jax-rs

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?

like image 449
slim Avatar asked May 11 '16 15:05

slim


People also ask

How do you set a list to default values?

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.


1 Answers

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));
    ....
}
like image 127
uniknow Avatar answered Oct 18 '22 19:10

uniknow