Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending array of int as @QueryParam in Jersey

Tags:

java

jersey

I am trying to write a GET method and send array of int's as QueryParam to it. This is how I am trying to do:

@GET
@Path("/test")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response test(@Context HttpServletRequest request,
        @QueryParam("list") final int list[])
{
    System.out.println(list.length);
    return Response.ok().build();
}

This results in 500 Internal Server error.When I tried using List of Integer, it worked perfectly. Doesn't Jersey support array as parameter or am I doing something wrong?

like image 530
doctore Avatar asked Oct 22 '25 06:10

doctore


1 Answers

The implementation of this in jersey which is not supporting array as a parameter. As you need to pass it as an array just pass it by converting the list to array by using Arrays.asList(arr)

The java doc say that the type of method parameter should be :

1) Be a primitive type;
2) Have a constructor that accepts a single String argument;
3) Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String))
4) Be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.

Sometimes parameters may contain more than one value for the same name. If this is the case then types in 4) may be used to obtain all values.

So using an array would not be possible in this case.

like image 167
Rishi Avatar answered Oct 24 '25 19:10

Rishi