如何在GET调用中将地图作为请求参数发送 [英] How to send map as request parameter in a GET call

查看:64
本文介绍了如何在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

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"

推荐答案

当将@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:

http://localhost:8080/test?a=abc&b=bcd

这篇关于如何在GET调用中将地图作为请求参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆