How to get Form data as a Map in Spring MVC controller?

java, spring, spring-mvc

Solution

You can also use `@RequestBody` with `MultiValueMap` e.g.

@RequestMapping(value="/create",
                method=RequestMethod.POST,
                consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createRole(@RequestBody MultiValueMap<String, String> formData){
 // your code goes here
}

Now you can get parameter names and their values.

MultiValueMap is in Spring utils package

Problem

I have a complicated html form that dynamically created with java script. I want to get the map of key-value pairs as a Map in java and store them. here is my controller to get the submitted data. ``` @RequestMapping(value="/create", method=RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public String createRole(Hashmap<String, Object) keyVals) { .... } ``` but my map is empty. How can i get form data as a map of name-value pairs in Spring mvc controller?

Original source