Spring @RequestParam Map <String, String> does not work in the POST method
from the Spring documentation, I accepted the following:
public @interface RequestParam
Annotations indicating that the method parameter should be bound to the web request parameter. Annotated handler methods are supported in Servlet and Portlet environments.
...
If the method parameter is Map or MultiValueMap, and the parameter name is not specified, then the map parameter is filled with all the names and values ββof the request parameters.
Now I have created a controller for testing. It has a GET and POST method, each of which uses the @RequestParam java.util.Map parameter only as a parameter. In the body of the method, I am only trying to print the size of the map. When I send requests (GET / POST) only in the GET method, the card contains any key / value pairs. I am using the Poster add-in in Firefox and I am posting options three .
import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @RequestMapping(value="test/method", method = RequestMethod.GET) @ResponseBody public String testmethodGet(@RequestParam Map<String, String> params) { System.out.println("GET: " + params.size()); // prints GET: 3 return ""; } @RequestMapping(value="test/method", method = RequestMethod.POST) @ResponseBody public String testmethodPost(@RequestParam Map<String, String> params) { System.out.println("POST: " + params.size()); // prints POST: 0 return ""; } } Do any of you know why @RequestParam Map will not work with a POST request or do I need to change something to make it work?
Thanks.
Actually, it works with the GET and POST method. It was my fault. Initially, this code will work when you actually pass parameters to the POST request.
Consider the following JS (jQuery) code to send a valid request:
$.ajax({ type: "POST", url: "test/method", data: { param1: param1, param2: param2, param3: param3 }, success: function(data) { console.log("testPost successful!"); }, dataType: "html", // expected return value type error: function(data, status, error) { console.log("testPost with errors!"); } }); Spring does not know how to convert a String received as request param to a map.
String-based values ββextracted from the request, including request parameters, path variables, request headers, and cookie values, may need to be converted to the target type of the method parameter or (for example, binding the request parameter to a field in @ModelAttribute) to which they are bound. If the target type is not String, Spring is automatically converted to the corresponding type. All simple types are supported, such as int, long, Date, etc. You can further customize the conversion process via WebDataBinder (see the "Configuring WebDataBinder Initialization" section) or Formatters registration using the FormattingConversionService (see Section 7.6, "Spring 3" Formatting the Field ").
Spring provides map support through HttpServletReqeust , which can be used to get a map through getParameterMap() .
@RequestMapping(value="test/method", method = RequestMethod.POST) @ResponseBody public String testmethodPost(HttpServletRequest request) { System.out.println("POST: " + request.getParameterMap().size()); // prints POST: 0 return ""; }