作为请求参数发送

作为请求参数发送

本文介绍了如何在 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"

I expected the received map to be like key1="a" value1=abc ,key2="b" value2="bcd"

推荐答案

这在 Spring MVC 指南:

@RequestParam 注释被声明为 MapMultiValueMap 参数时,映射是填充了所有请求参数.

这意味着您当前得到的响应是预期的结果.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,但您必须将请求更改为:

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 调用中将地图(键值对)作为请求参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:27