java 处理其余 Web 服务 url 中单个查询参数的多个值并使用休眠条件处理它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12411592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling multiple values for a single query parameter in a rest webservice url and handling them using hibernate criteria
提问by Npa
I have a rest web service which would accept 'name' and fetch the tokens from the db with this name and return the tokens back. The url for the rest webservice would be:
我有一个休息 Web 服务,它会接受“名称”并使用此名称从数据库中获取令牌并将令牌返回。其余网络服务的网址为:
http://localhost:8080/NameService/Tokens?name=Bob
and in the serviceLayer, I have my method as follows:
在 serviceLayer 中,我的方法如下:
@GET
@Path("Tokens")
@Produces("application/xml")
public JAXBElement<GetToken> getokenByName(@QueryParam("name") final String name ) {
if(name!=null){
// use hibernate criteria to fetch the records from db
Criteria crit = getSession().createCriteria(getPersistentClass());
crit.add(Restrictions.eq("name",name))
}
}
Now we have a change in requirement when the client can send multiple names in the request at the same time.For example, give me the tokens where name = "bob" or "brendon" or "aaron" The URL could be like this:
现在当客户端可以在请求中同时发送多个名称时,我们的需求发生了变化。例如,给我令牌 where name = "bob" or "brendon" or "aaron" 的 URL 可能是这样的:
http://localhost:8080/NameService/Tokens?name=Bob,Aaron,Brendon
The number of names sent is variable. It can be 1 or 2 or 3 so on. Any ideas on how to achieve this split and passing them to hibernate criteria as an OR condition in the service layer by tweaking the code shown above, which works for one name?
发送的姓名数量是可变的。它可以是 1 或 2 或 3,依此类推。关于如何通过调整上面显示的代码来实现这种拆分并将它们作为服务层中的 OR 条件传递给休眠条件的任何想法,这适用于一个名称?
One way might be to use StringUtils
to parse out the different names using delimiter (comma) and then form a list and some how adding the names in the list to criteria restrictions. But not sure if this works.
一种方法可能是StringUtils
使用分隔符(逗号)解析不同的名称,然后形成一个列表以及如何将列表中的名称添加到条件限制中。但不确定这是否有效。
Any ideas?
有任何想法吗?
回答by Alex
Indeed the easiest way would be to split the query param using split(",")
.
事实上,最简单的方法是使用split(",")
.
You can also set your query param to a List<String>
and pass parameters like this:
您还可以将查询参数设置为 aList<String>
并传递如下参数:
http://localhost:8080/NameService/Tokens?name=Bob&name=Aaron&name=Brendon
Or you could also implement your own StringReaderProvider
and register it to jersey.
或者您也可以实现自己的StringReaderProvider
并将其注册到球衣。
For your comment:you should use a disjunction.
对于您的评论:您应该使用析取。
@GET
@Path("Tokens")
@Produces("application/xml")
public JAXBElement<GetToken> getokenByName(@QueryParam("name") final String names) {
if (names != null) {
Criteria crit = getSession().createCriteria(getPersistentClass());
Disjunction disjunction = Restrictions.disjunction();
for (String name: names.split(",")) {
disjunction = disjunction.add(Restrictions.eq("name", name));
}
crit.add(disjunction);
}
...
}
回答by basiljames
Your idea of splitting the string will work. You can also check how some REST services are done in industry. Check the linkedin people search API
您拆分字符串的想法会奏效。您还可以检查一些 REST 服务在行业中是如何完成的。检查linkedin 人物搜索API
Check the flexibility of the @Path
annotation using regex.
@Path
使用正则表达式检查注释的灵活性。