我有 Rest WS,其路径参数如下
@Path("/myService/{types}")
public Response myFunction(@Context HttpServletRequest request
,@PathParam("types")String[] types)
{
Criteria criteria = session.createCriteria(myObject.class);
criteria.add(Restrictions.in("tranType", types));
List<myObject> lst= (List<myObject>) criteria.list()
}
或者可能有 ArrayList 或 List 以及 String[]
我正在尝试从浏览器调用此 WS。当我使用数组中的 1 个参数调用它时,它工作正常。 WS 调用类似于 /myService/type1
。
但是当我想用数组中的 2 个或更多参数来调用它时,它不起作用。响应是空数组,但它必须返回填充数组。我正在使用许多参数调用 WS,例如 /myService/type1,type2
但它不起作用,我也尝试了 /myService/{type1,type2}
和 /myService/[type1,type2]
和 /myService/["type1","type2"]
和 /myService/"type1","type2"
但是不幸的是没有任何作用。你能告诉我该怎么做吗?
最佳答案
正如我在评论中提到的,接受路径参数为 String
的最简单方法,然后根据代码中的分隔符(如 ,
)分割它。
下面是您修改后的代码,显示了上述机制:-
@Path("/myService/{types}")
public Response myFunction(@Context HttpServletRequest request
,@PathParam("types")String type)
{
Criteria criteria = session.createCriteria(myObject.class);
String [] types = type.split(",");
criteria.add(Restrictions.in("tranType", types));
List<myObject> lst= (List<myObject>) criteria.list()
}
如果您还有其他问题,请告诉我。
关于java - 如何从浏览器将列表(数组)参数传递给 Rest Ws,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754585/