Spring MVC + RequestParam as Map + get parameters of an array of URLs not working

I am currently working on an API controller. This controller should catch any unknown parameter and place it in the Map object. Now I use this code to catch all the parameters and put them in a Map

 public String processGetRequest(final @RequestParam Map params) 

Now the address I'm calling looks like this:

 server/api.json?action=doSaveDeck&Card_IDS[]=1&Card_IDS[]=2&Card_IDS[]=3 

Then I print out the parameters for debugging purposes using this piece of code:

 if (logger.isDebugEnabled()) { for (Object objKey : params.keySet()) { logger.debug(objKey.toString() + ": " + params.get(objKey)); } } 

Result:

 10:43:01,224 DEBUG ApiRequests:79 - action: doSaveDeck 10:43:01,226 DEBUG ApiRequests:79 - Card_IDS[]: 1 

But the expected result should look something like this:

 10:43:XX DEBUG ApiRequests:79 - action: doSaveDeck 10:43:XX DEBUG ApiRequests:79 - Card_IDS[]: 1 10:43:XX DEBUG ApiRequests:79 - Card_IDS[]: 2 10:43:XX DEBUG ApiRequests:79 - Card_IDS[]: 3 

Or at least tell me that Card_IDS is a String[] / List<String> and therefore should be running. I also tried manually changing the List<String> parameter, but this also does not work:

 for (Object objKey : params.keySet()) { if(objKey.equals(NameConfiguration.PARAM_NAME_CARDIDS)){ //Never reaches this part List<String> ids = (List<String>)params.get(objKey); for(String id : ids){ logger.debug("Card: " + id); } } else { logger.debug(objKey + ": " + params.get(objKey)); } } 

Can someone tell me how to get the Card_IDS array - It should be dynamically - from Map params using NameConfiguration.PARAM_NAME_CARDIDS ?

+7
source share
1 answer

When you request a Map annotated using @RequestParam Spring, a map is created containing all the name / value pairs of the request parameter. If there are two pairs with the same name, then there can only be one on the map. So essentially a Map<String, String>

You can access all parameters through MultiValueMap :

 public String processGetRequest(@RequestParam MultiValueMap parameters) { 

This map is essentially a Map<String, List<String>> . Therefore, parameters with the same name will be in the same list.

+15
source

All Articles