Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send map(key-value pair) as request parameter in a GET call

Is it possible to send map as parameter in a GET call.? i searched and i could find for list and set collection. But did not find anything for map collection.

I tried the following, My controller method looks like this.

@GetMapping("/test")
    public ResponseEntity<?> mapTest(@RequestParam Map<String,String> params) {

        LOG.info("inside test with map  "+  params );

        return new ResponseEntity<String>("MAP", HttpStatus.OK);
    }

And i sent the following request from postman

http://localhost:8080/test?params={a:abc,b:bcd}

Everything works without errors and exceptions. But the map which i received looks like key=params , value={a:abc,b:bcd}

I expected the received map to be like key1="a" value1=abc ,key2="b" value2="bcd"

like image 208
Arun Gowda Avatar asked Feb 16 '18 09:02

Arun Gowda


People also ask

How to send query parameters in GET request Postman?

To send a query parameter, add it directly to the URL or open Params and enter the name and value. When you enter your query parameters in either the URL or the Params fields, these values will update everywhere they're used in Postman. Parameters aren't automatically URL-encoded.

How to send multiple parameters in GET request in Postman?

Multiple ParametersIn the above URL, '&' should be followed by a parameter such as &ie=UTF-8. In this parameter, i.e., is the key and, UTF-8 is the key-value. Enter the same URL in the Postman text field; you will get the multiple parameters in the Params tab.

How do you request parameters in Spring?

Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.

How do I get all request parameters?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.


1 Answers

This is documented in the Spring MVC guide:

When an @RequestParam annotation is declared as Map<String, String> or MultiValueMap<String, String> argument, the map is populated with all request parameters.

This means that the response you currently get is the expected result. The Map contains a list of all parameters, and in your case, you only have a single parameter called param.

If you need a custom parameter mapping, you'll have to implement it by yourself. Since you're not using JSON either, you probably have to manually parse the parameter.

However, if your goal is to have a dynamic map of parameters, you can still use the Map<String, String>, but you'll have to change your request into:

http://localhost:8080/test?a=abc&b=bcd
like image 176
g00glen00b Avatar answered Nov 14 '22 23:11

g00glen00b