其他分享
首页 > 其他分享> > Spring MVC填充了@RequestParam Map

Spring MVC填充了@RequestParam Map

作者:互联网

我的Spring MVC @Controller中有以下方法:

@RequestMapping(method = RequestMethod.GET)
public String testUrl(@RequestParam(value="test") Map<String, String> test) {   
    (...)
}

我称之为:

http://myUrl?test[A]=ABC&test[B]=DEF

但是,“test”RequestParam变量始终为null

为了填充“test”变量我该怎么办?

解决方法:

详情如下
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

如果方法参数是Map或MultiValueMap并且未指定参数名称,则使用所有请求参数名称和值填充map参数.

所以你会改变你的定义.

@RequestMapping(method = RequestMethod.GET)
public String testUrl(@RequestParam Map<String, String> parameters) 
{   
  (...)
}

如果您调用了网址http://myUrl?A=ABC&B=DEF,则在您的参数中

你将拥有自己的方法

parameters.get("A");
parameters.get("B");

标签:data-binding,parameters,spring,spring-boot-2,populate
来源: https://codeday.me/bug/20190622/1263627.html