我的JAX-RS(Jersey)服务中有一个HTTP GET方法,但我希望它接受动态数量的查询参数。不过,我想使用Jenkins API将这些参数包装在@BeanParam
实体中,但是懒得,像这样:
@GET
public <T extends MyMessage> Response get(@PathParam("entity") String entity, @Context Request request) {
/* Get the class that would normally be a parameter of this method, like this:
* [...] get(@PathParam("entity") String entity, @BeanParam MyMessage m)
*/
Class<T> clazz = getMessageClass(entity);
/* That's the magic part I would like to know how to do: */
T message = createMessageFromRequest(request, clazz);
/* ... then I would deal with my message however I wanted. */
}
我知道我可以注入UriInfo
并“手动”读取queryParams的地图,但我的观点是使用Jersey API来保持组织有序并且不必手动将字符串转换为正确的参数类型。
就像我的例子一样,我的getMessageClass(entity)
看起来像这样:
switch(entity) {
case 'foo':
return FooSpecificGet.class;
default:
return CommonGet.class;
}
和班级:
class CommonGet {
@QueryParam("page")
private int pageNumber;
@QueryParam("pageSize")
private int pageSize;
...
}
class FooSpecificGet extends CommonGet {
@QueryParam("fooFilter")
private String fooFilter;
...
}