在 jax-rs 中获取未命名的查询参数
请求可以像这样进入服务器:
http://localhost:8080/App/rest /data/?sort(+Browser)) (来自 dojo 数据网格)
我想获取 ?sort(+Browser) 部分,问题是这是关键值,但是。每次都可能不同(例如, ?sort(-username) 等)目前,我这样做:
@GET
@Produces( { MediaType.APPLICATION_JSON })
public Response getData(@Context UriInfo ui) throws NamingException {
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
for (Entry<String, List<String>> entry : queryParams.entrySet()){
if( Pattern.matches("sort.*", entry.getKey()) ){
//do something with entry.getKey();
}
}
...
有没有比使用 for 循环更好/更简单的方法来获取此查询参数?而不是使用 @Context UriInfo ui 我以为我可以将 QueryParam 与正则表达式一起使用,例如
@GET
@Produces( { MediaType.APPLICATION_JSON })
public Response getData(@QueryParam("{sort.*}") String sortStr) throws NamingException { ...}
但是,它看起来不像 QueryParam 接受任何形式的正则表达式,
我也尝试将名称字段留空,但后来意识到排序(+浏览器)将是键,因此将其留空并没有帮助。 谢谢 :)
Requests can come into the server like:
http://localhost:8080/App/rest/data/?sort(+Browser) (from dojo data grid)
I want to grab the ?sort(+Browser) part. The problem is that this is the key value, but it can be different every time (eg, ?sort(-username), etc) Currently, I am doing it like this:
@GET
@Produces( { MediaType.APPLICATION_JSON })
public Response getData(@Context UriInfo ui) throws NamingException {
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
for (Entry<String, List<String>> entry : queryParams.entrySet()){
if( Pattern.matches("sort.*", entry.getKey()) ){
//do something with entry.getKey();
}
}
...
Is there a better/easier way to get this query parameter than using the for loop? Instead of using @Context UriInfo ui
I thought I could use the QueryParam with a regex, something like
@GET
@Produces( { MediaType.APPLICATION_JSON })
public Response getData(@QueryParam("{sort.*}") String sortStr) throws NamingException { ...}
However, it doesn't look like QueryParam accepts any form of regex.
I also tried leaving the name field blank, but then realized that the sort(+Browser) would be the key, so leaving it blank didn't help.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许做最简单的事情 - 直接使用 HTTP 请求:
Maybe do the simplest thing - use HTTP request directly: