在 Jersey 中将 int 数组作为 @QueryParam 发送

doc*_*ore 4 java jersey

我正在尝试编写一个 GET 方法并将 int 数组作为 QueryParam 发送给它。这就是我正在尝试做的事情:

@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();
}
Run Code Online (Sandbox Code Playgroud)

这会导致 500 内部服务器错误。当我尝试使用整数列表时,它工作得很好。Jersey 不支持数组作为参数还是我做错了什么?

Ris*_*shi 5

jersey 中的实现不支持数组作为参数。由于您需要将其作为数组传递,只需通过使用将列表转换为数组来传递它Arrays.asList(arr)

java doc说方法参数的类型应该是:

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.
Run Code Online (Sandbox Code Playgroud)

有时,参数可能包含多个同名值。如果是这种情况,则可以使用 4) 中的类型来获取所有值。

因此在这种情况下使用数组是不可能的。