4

from the Spring documentation I took the following:

public @interface RequestParam

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

...

If the method parameter is Map or MultiValueMap and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

Now I have created a controller for test purposes. It has a GET and a POST method and each uses a @RequestParam java.util.Map as only parameter. In the method body I am only trying to print the size of the map. When I send requests (GET/POST) only in the GET method the map contains any key/value pairs. I am using the Poster add-on in Firefox and I am sending three parameters.

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

    }

}

Would any of you guys know why @RequestParam Map would not work with a POST request or whether I need to change something to make it work?

Thanks.

2 Answers 2

8

Actually, it works on GET and POST method. It was solely my fault. The initially given code will work, when you actually pass parameters to the POST request.

Consider the following JS ( jQuery ) code as how 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!");
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

do you know how to send from postman instead of ajax for testing purpose ?
@ArunGowdru, since this is JSON is should be pretty straight forward. Endpoint would be yourhost/api/test/method and the request body would be { "param1": "value", "param2": "value" }.
2

Spring does not know how to convert a String received as a 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 field (e.g., binding a request parameter to a field in an @ModelAttribute parameter) they're bound to. If the target type is not String, Spring automatically converts to the appropriate type. All simple types such as int, long, Date, etc. are supported. You can further customize the conversion process through a WebDataBinder (see the section called “Customizing WebDataBinder initialization”) or by registering Formatters with the FormattingConversionService (see Section 7.6, “Spring 3 Field Formatting”).

Spring does provide support for maps via the HttpServletReqeust which could be used to obtain the map via 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 "";

    }

1 Comment

Thanks, for your answer. Just to make sure. I am using the map, because I want to get a map of String key/value pairs. I am not expecting one of the parameters to be a map. And MultiValueMap is only a map as well ie Map<K,List<V>>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.