开发者

get unnammed query parameter in jax-rs

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开发者_如何学C();
    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 :)


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜