在 jax-rs 中获取未命名的查询参数

发布于 2024-11-15 05:26:14 字数 1081 浏览 3 评论 0原文

请求可以像这样进入服务器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-11-22 05:26:14

也许做最简单的事情 - 直接使用 HTTP 请求:

public Response getData(@Context HttpServletRequest req) throws NamingException {
  String qParams = req.getQueryString();
  //apply regexp to get needed parameter from qParams
}

Maybe do the simplest thing - use HTTP request directly:

public Response getData(@Context HttpServletRequest req) throws NamingException {
  String qParams = req.getQueryString();
  //apply regexp to get needed parameter from qParams
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文