REST查询字符串参数

时间:2015-07-26 07:19:46

标签: java rest jersey

我有从客户端发送的以下查询字符串参数

{"take":75,"skip":0,"page":1,"pageSize":75,"filter":{"logic":"and","filters":
[{"field":"prodCode","operator":"eq","value":"Z20"}]}}:

在REST服务器中,我如何才能收到上述格式并正确分配给每个类别?

更新1

MultivaluedMap params = uriInfo.getQueryParameters();

参数的价值是

  
    

{_ = [1437904506062],{“take”:75,“skip”:0,“page”:1,“pageSize”:75,“filter”:{“logic”:“and”,“过滤器“:[{” 字段 “:” prodCode”, “运算符”: “当量”, “值”: “Z30”}]}} = []}

  

1 个答案:

答案 0 :(得分:2)

这是一个查询参数,因此您必须使用值key的{​​{1}}来获取它。因此,使用{"take":75,"skip":0,"page":1,"pageSize":75,"filter":{"logic":"and","filters": [{"field":"prodCode","operator":"eq","value":"Z20"}]}}:,将上述内容存储在key中,然后解析为String(我在这里使用JSON)。您可以根据需要提取任何org.json / key。您可以使用代码段: -

value

输出: -

String inputValue = @QueryParam(YOUR_KEY); // OR whatever way you get it
        /*
         * This inputValue will actually contain your value :-
         * {"take":75,"skip":0,"page":1,
         * "pageSize":75,"filter":{"logic":"and","filters":
         * [{"field":"prodCode","operator":"eq","value":"Z20"}]}}
         */

        JSONObject inputJSON = new JSONObject(inputValue); 
        //Now getting values from input JSON
        int take = inputJSON.getInt("take");
        int skip = inputJSON.getInt("skip");
        int page = inputJSON.getInt("page");
        int pageSize = inputJSON.getInt("pageSize");
        JSONObject filter = inputJSON.getJSONObject("filter"); // filter is again a JSONObject 
        String logic = filter.getString("logic");
        System.out.println(take + " "+skip + " "+page + " "+pageSize + " "+logic);
        JSONArray filters = filter.getJSONArray("filters"); // filters is a JSONArray

        for(int i = 0; i< filters.length(); i++){  // iterating over JSONArray 
        JSONObject jo = (JSONObject)filters.get(i); 
        String field = jo.getString("field"); 
        String operator = jo.getString("operator"); 
        String value = jo.getString("value"); 
        System.out.println(field + " "+operator + " "+value ); 
}