我正在尝试设置一个允许用户从数据库中检索多个条目的服务。为了允许用户指定他们想要查看的条目,我正在为id号传递varargs:
public Person getPersons(int... ids) {
//my code here
}
如何设置它以使用@QueryParam或@FormParam?我试过这样设置:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/search")
public Person getPersons(@QueryParam("id") int... ids) {
//my code here
}
因此,如果参数中包含id编号1和5,则将返回这些参数配置文件的条目。
但是,这只是ids变量的错误,具有以下注意事项:
Checks types of parameters annotated @PathParam, @QueryParam, etc. The type of the annotated parameter, field or property must either:
Be a primitive type.
Have a constructor that accepts a single String argument.
Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String)).
Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance capable of a "from string" conversion for the type.
Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2, 3 or 4 above. The resulting collection is read-only.
答案 0 :(得分:1)
您需要使用不同的值多次传递相同的查询参数
id=1&id=2&id=3
然后你可以阅读数组中的ID列表:
public Response receiveListOfID(@QueryParam("id") final List<String> idList) {
....
}