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

Try this,

@RequestMapping(value = "/create", method = RequestMethod.POST, 
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createRole(@RequestParam HashMap<String, String> formData) {

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


The answers above already correctly point out that @RequestParam annotation is missing. Just to add why thta is required,

A simple GET request would be soemthing like :

http://localhost:8080/api/foos?id=abc

In controller, we need to map the function paramter to the parameter in the GET request. So we write the controller as

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam String id) {
    return "ID: " + id;
}

and add @RequestParam for the mapping of the paramter "id".


I,ve just found a solution

@RequestMapping(value="/create", method=RequestMethod.POST, 
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createRole(HttpServletRequest request) {
    Map<String, String[]> parameterMap = request.getParameterMap();
    ...
}

this way i have a map of submitted parameters.