是否可以在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
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"
推荐答案Spring MVC指南:
当将@RequestParam批注声明为Map<String, String>或MultiValueMap<String, String>参数时,该地图将填充所有请求参数.
When an @RequestParam annotation is declared as Map<String, String> or MultiValueMap<String, String> argument, the map is populated with all request parameters.
这意味着您当前收到的响应是预期的结果. 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:
localhost:8080/test?a=abc&b=bcd更多推荐
如何在GET调用中将地图作为请求参数发送
发布评论