【问题标题】:How to get a list of param in a Map from @RequestParam in Spring boot如何在 Spring Boot 中从 @RequestParam 获取 Map 中的参数列表
【发布时间】:2017-03-27 11:41:01
【问题描述】:

我有这个端点(删除了所有不相关的)并且想看看如何直接从参数映射中获取文件名列表:

  • 第一个@RequestParam 是使“文件”参数成为必需的,
  • 第二个@RequestParam 是将所有参数放入地图中,而不是单独放入。

即:http://localhost:8080/files?file=first&file=second&file=third

@RequestMapping(path = "/files",
                method = RequestMethod.GET)
public void getFiles(@RequestParam(value = "file",
                                   required = true) String[] files,
                     @RequestParam Map<String, Object> params) {
    for (String file : files) { // this works, all get printed
        logger.info("file: " + file);
    }

    // this, however, only gives the first entry not the whole list
    //
    logger.info("params.get(file): " + params.get("file")); 
}

【问题讨论】:

  • 问题是:如何为地图中的每个值定义一个键?这似乎有点困难,因为您的文件来自请求。

标签: spring-boot


【解决方案1】:

一个地图不能有多个具有相同键的值。由于多个参数同名(file),因此只保留一个值。

要处理具有相同键的多个参数的查询,请使用MultiValueMap&lt;String, Object&gt;。多值映射是

存储多个值的 Map 接口的扩展。

【讨论】:

    猜你喜欢
    • 2022-07-12
    • 2021-06-10
    • 2018-07-11
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多