如何在GET调用中将地图作为请求参数发送

编程入门 行业动态 更新时间:2024-10-28 00:24:57
本文介绍了如何在GET调用中将地图作为请求参数发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在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调用中将地图作为请求参数发送

本文发布于:2023-10-29 13:26:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   参数   地图   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!