0

有没有办法解析/解组查询字符串,将数组作为 Java 中的参数传递到 Map 或自定义类中?

网址示例:

localhost:8080/app/ws/categories?take=10&skip=10&page=2&pageSize=10&filter[logic]=and&filter[filters][0][field]=company_id&filter[filters][0][operator]=eq&filter[filters][ 0][值]=1513

我需要像这样解析过滤器参数:

public class Filter {

private String logic;
private List<Filters> filters;

public class Filters {
    private String field;
    private String operator;
    private String value;
    }
}

还有一点 - 我在这里使用 Jersey,也许有可能使用Jersey 的工具解析/解组@QueryParam ?

无论如何,任何其他 Java 解决方案也可以。

4

2 回答 2

0

我们可以使用 JAXRS 2.0 规范中的 @BeanParam 来做到这一点

public PageableResponse<PatientBundleType> getPatientBundles(@BeanParam final PageableBeanParam paging)
{
    ...
}

public class PageableBeanParam
{
    public PageableBeanParam(@Context final UriInfo uriInfo)
    {

        for (Entry<String, List<String>> param : params.entrySet())
        {
            String key = param.getKey();
            String value = param.getValue().iterator().next();

            if ("pageSize".equals(key))
            {
                setPageSize(Integer.valueOf(value));
            }

            ...
        }
    }
}
于 2014-06-26T17:16:50.383 回答
0

I would suggest 2 things :

Either you want to perform a GET, then to keep a RESTful approach, I would have an Url like : localhost:8080/app/ws/categories?take/10/skip/10/page/2/pageSize/10/filter/logic/and/ ...etc

Or POST is acceptable, and I would send JSON payload

于 2012-04-18T09:30:06.863 回答