How to accept list of parameter values as input in REST?

1.5k views Asked by At

I want to display info about Subdepartments using multiple id's provided.This is what I have. Now, what would an example of url with multiple parameters? I tried comma, slash but didnt work

@Path( "{id: .*}" )
@GET
public Collection< SubDepartmentDTO > findMultiple ( @PathParam ( "id" ) List<Integer> idList ) {
    Map< Integer, SubDepartmentDTO > subList = new LinkedHashMap<>();

    int index = 0;
    while( index < idList.size() ){
        subList.put( idList.get(index), SubDepartmentDAO.findOne ( idList.get(index) ) );
        index++;
    }
    return subList.values();
}
1

There are 1 answers

0
Wabi On

You cant use an array as path parameter, see the docs, in particular:

The type of the annotated parameter, field or property must either:

  • Be PathSegment, the value will be the final segment of the matching part of the path. See UriInfo for a means of retrieving all request path segments.
  • Be List, the value will be a list of PathSegment corresponding to the path segment(s) that matched the named template parameter. See UriInfo for a means of retrieving all request path segments.
  • Be a primitive type.
  • Have a constructor that accepts a single String argument.
  • Have a static method named valueOf that accepts a single String argument (see, for example, Integer.valueOf(String)).

If what you are trying to achieve /resource/id/subid/subsubid then answer to this question will help you.

However, if this hierarchy doesn't make sense in your case and you want to get list of departments on the same level (i.e. subid1, subid2, etc), maybe you want to use query parameter instead of path parameter.