You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中如何将java.util.Map作为@RestController方法参数?

问题

在Spring Boot项目中,编写了如下@RestController方法:

@RestController
public class Controller {

    @GetMapping("/test/map")
    public void testMap(@RequestParam String params, @RequestParam List<String> list, @RequestParam(name = "map") Map<String, String> map) {

        System.out.println("map >>> " + map);
        System.out.println("params >>> " + params);
        System.out.println("list >>> " + list);
    }

}

通过URL https://localhost:8080/test/map?params=stringParam&list=va1,val2,val3&map=param1:val1,param2:val2 发送请求时,出现错误:

org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map'

需要实现正确解析单个参数形式的java.util.Map。

解决方案

方法1:使用Spring默认支持的Map参数格式

Spring原生支持直接解析Map类型请求参数,只需遵循以下格式之一,无需额外编码:

  • 格式1:用方括号指定Map的key,重复参数名
    https://localhost:8080/test/map?params=stringParam&list=va1,val2,val3&map[param1]=val1&map[param2]=val2
    
  • 格式2:用点分隔Map的key
    https://localhost:8080/test/map?params=stringParam&list=va1,val2,val3&map.param1=val1&map.param2=val2
    

以上两种格式会被Spring自动解析为Map<String, String>,直接注入方法参数。

方法2:自定义Converter解析单个字符串参数

如果必须使用map=param1:val1,param2:val2这种单个参数格式,可通过自定义转换器实现字符串到Map的转换:

  1. 编写自定义Converter类:
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

@Component
public class StringToMapConverter implements Converter<String, Map<String, String>> {

    @Override
    public Map<String, String> convert(String source) {
        Map<String, String> map = new HashMap<>();
        if (source == null || source.isEmpty()) {
            return map;
        }
        // 按逗号分割键值对
        String[] keyValuePairs = source.split(",");
        for (String pair : keyValuePairs) {
            // 按冒号分割键和值,限制分割次数避免值中包含冒号的情况
            String[] keyValue = pair.split(":", 2);
            if (keyValue.length == 2) {
                map.put(keyValue[0].trim(), keyValue[1].trim());
            }
        }
        return map;
    }
}
  1. 注册Converter到Spring MVC配置:
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private StringToMapConverter stringToMapConverter;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(stringToMapConverter);
    }
}

完成配置后,原URL即可正常解析出Map参数。

内容的提问来源于stack exchange,提问作者user18503943

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 17:47:25