How to get query param as HashMap
Asked Answered
B

3

7

I want to be able pass to my server a GET request suh as:

 http://example.com/?a[foo]=1&a[bar]=15&b=3

And when I get the query param 'a', it should be parsed as HashMap like so:

{'foo':1, 'bar':15}

EDIT: Ok, to be clear, here is what I am trying to do, but in Java, instead of PHP:

Pass array with keys via HTTP GET

Any ideas how to accomplish this?

Biddick answered 4/3, 2013 at 8:41 Comment(0)
M
2

There is no standard way to do that.

Wink supports javax.ws.rs.core.MultivaluedMap. So if you send something like http://example.com/?a=1&a=15&b=3 you will receive: key a values 1, 15; key b value 3.

If you need to parse something like ?a[1]=1&a[gv]=15&b=3 you need to take the javax.ws.rs.core.MultivaluedMap.entrySet() and perform an additional parsing of the keys.

Here's example of code you can use (didn't test it, so it may contain some minor errors):

String getArrayParameter(String key, String index,  MultivaluedMap<String, String> queryParameters) {
    for (Entry<String, List<String>> entry : queryParameters.entrySet()) {
        String qKey = entry.getKey();
        int a = qKey.indexOf('[');
        if (a < 0) {
            // not an array parameter
            continue;
        }
        int b = qKey.indexOf(']');
        if (b <= a) {
            // not an array parameter
            continue;
        }
        if (qKey.substring(0, a).equals(key)) {
            if (qKey.substring(a + 1, b).equals(index)) {
                return entry.getValue().get(0);
            }
        }
    }
    return null;
}

In your resource you should call it like this:

@GET
public void getResource(@Context UriInfo uriInfo) {
    MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
    getArrayParameter("a", "foo", queryParameters);
}
Marleen answered 5/3, 2013 at 7:23 Comment(7)
This is not what I want, because keys are also important to me. So when I get 'a' , I want to it's value to be: {'1':1, 'gv':1}Biddick
Your proposed format is not usual query parameters format: you mix the key name with index. Anyway you can get the keySet() from the map and parse it yourself.Marleen
This was the initial idea - is there something which I can use instead of parsing it on my own :)Biddick
The format of a query path is ?key=value&key=value&key=value... this is the normal format and this how it should be used. If you are trying to use it differently, it's up to you, but it's not standard.Marleen
Anyway, using a MultivaluedMap.entrySet() will help you. You'll get the parameters already parsed. You will only need to go over the keys and see if they contain '[]' so you can build your own map.Marleen
I edited the post. Once again - I am trying to avoid that custom parsing. I gave a link with such thing done in PHP, I have seen it in Rails too, my question is - "Is there such thing in Java?"Biddick
I also edited the answer. The very short answer is "No". The little longer: "No standard way" Sorry if the answer doesn't help you much.Marleen
P
1

You can pass JSON Object as String by appending it to URL. But befor that you need to encode it you can use the method given in this link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm for encoding and then just append the json object as any other string

Pontificals answered 18/3, 2013 at 9:29 Comment(1)
My problem is not constructing the url, but parsing it.Biddick
P
0

Alternatively if you are using HttpServletRequest then you can use getParameterMap() method which gives you all the parameters and its values as map.

e.g.

public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws IOException {

   System.out.println("Parameters : \n"+request.getParameterMap()+"\n End");

}
Pontificals answered 18/3, 2013 at 9:53 Comment(10)
I don't think you understand what I want. Please read the post again. Also, the linked answer I gave - the implementation in PHPBiddick
The URL which you are mentioning is formed by your code or you are just receiving it? As if you can change the URL creation as example.com/?foo=1&bar=15&b=3 and then use above code you will get the map with keys as foo, bar..etc and values as 1, 15...etcPontificals
Ok, but how can I distinguish that foo and bar are keys of a and b is not?Biddick
one way is keep fix format i.e. the last parameter is b or first parameter is b.Pontificals
Another way is pass 'a' as JSONObject(e.g.www.example.com/?a={'foo':1, 'bar':15}) and them use request.getParameter("a") and convert the json into Map using the code given in link : techtamasha.com/convert-json-to-map-in-java/168Pontificals
Well, the purpose was to not use any third party library, or parse it in anyway after I got the parameter. The idea was to get it already parsed by just calling .get('a') and receive a HashMap. Anyway, it doesn't seem to exist such way in Java.Biddick
Well then if you know the URL format then you can do that. like if example.com/?foo=1&bar=15&b=3 this is the URL then call Map map= request.getParameterMap() and then map.remove(b), then the remaining map is what you needed.Pontificals
Consider that I do not know the format, just that a hashmap 'a' will be given.Biddick
Then there is no standard way.either use json or pass the request parameter with specific format and remove the parameter from the map which do not satisfy the pattern like create url example.com/?a#foo=1&a#bar=15&b=3 then then after Map map= request.getParameterMap() iterate over the map and remove the entries whose keys does not satisfy the pattern start with "a#"Pontificals
Thanks, I was already told there is no standard way. My research ends here.Biddick

© 2022 - 2024 — McMap. All rights reserved.