33

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?

bdkosher
  • 5,753
  • 2
  • 33
  • 40
Morteza Adi
  • 2,413
  • 2
  • 22
  • 37
  • 2
    Add `@RequestParam` to your method argument. – M. Deinum Jul 03 '14 at 11:05
  • I donno what the param name is! i found a solution to my problem posted below. – Morteza Adi Jul 03 '14 at 11:08
  • You don't need a param name just the annotation. `@RequestParam` on the type map will give you all request parameters. – M. Deinum Jul 03 '14 at 11:41
  • yeah it did a job;I've just tested your solution everything is ok and definitely better solution tan mine. But there is a problem multiselect values just returns first selected item! not all of the selected values!! when i get the params from HttpServletRequest multiselects works fine! any idea?? – Morteza Adi Jul 03 '14 at 12:11
  • 2
    Instead of `Map` use `MultiValueMap`. – M. Deinum Jul 03 '14 at 12:17

4 Answers4

52

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

bdkosher
  • 5,753
  • 2
  • 33
  • 40
optional
  • 3,260
  • 5
  • 26
  • 47
  • 4
    I am getting "Content Type is not supported" issue – Ram Dec 24 '17 at 08:30
  • 1
    Make sure the Content-type is application/x-www-form-urlencoded, that got rid of the content type not supported. – JSeven Apr 18 '18 at 16:25
  • 2
    On my side, I get stuck since I've migrated to Spring Boot 2.6. There is no data in received @RequestBody variable. – Damien C Dec 23 '21 at 16:01
9

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.

Morteza Adi
  • 2,413
  • 2
  • 22
  • 37
1

Try this,

@RequestMapping(value = "/create", method = RequestMethod.POST, 
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createRole(@RequestParam HashMap<String, String> formData) {
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
1

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".

Cranberry
  • 13
  • 3
  • I like having the "why", thanks for that. your explanation would be improved if it were in the context of the OP's question, specifically, a POST request. – user3481644 Nov 28 '21 at 13:42