[英]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.?是否可以在 GET 调用中将地图作为参数发送。? 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}
但是我收到的地图看起来像
key=params , value={a:abc,b:bcd}
I expected the received map to be like key1="a" value1=abc ,key2="b" value2="bcd"
我希望收到的地图像
key1="a" value1=abc ,key2="b" value2="bcd"
This is documented in the Spring MVC guide :这记录在Spring MVC 指南中:
When an
@RequestParam
annotation is declared asMap<String, String>
orMultiValueMap<String, String>
argument, the map is populated with all request parameters.当
@RequestParam
注释被声明为Map<String, String>
或MultiValueMap<String, String>
参数时,地图将填充所有请求参数。
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
. Map
包含所有参数的列表,在您的情况下,您只有一个名为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.
由于您也没有使用 JSON,因此您可能必须手动解析参数。
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:但是,如果您的目标是获得参数的动态映射,您仍然可以使用
Map<String, String>
,但您必须将请求更改为:
http://localhost:8080/test?a=abc&b=bcd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.