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"
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.
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.
Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.
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.
This is documented in the Spring MVC guide:
When an
@RequestParam
annotation is declared asMap<String, String>
orMultiValueMap<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With