问题描述
是否可以在GET调用中将地图作为参数发送?我搜索了,我可以找到列表并设置集合.但是没有找到要收集地图的任何东西.
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}
一切正常,没有错误和异常.但是我收到的地图看起来像 key = params,value = {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}
我希望收到的地图类似于 key1 ="a" value1 = abc,key2 ="b" value2 ="bcd"
I expected the received map to be like key1="a" value1=abc ,key2="b" value2="bcd"
推荐答案
这意味着您当前收到的响应是预期的结果. Map
包含所有参数的列表,在您的情况下,您只有一个名为 param
的参数.
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
.
如果需要自定义参数映射,则必须自己实现.由于您也不使用JSON,因此您可能必须手动解析参数.
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.
但是,如果您的目标是要有一个动态的参数映射,则仍然可以使用 Map< String,String>
,但是您必须将请求更改为:
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
这篇关于如何在GET调用中将map(键值对)作为请求参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!