1
votes

I have a web request like

/path?param=value1&param=value2

which maps to a List<String> param on the Controller side.

The important aspect to note is that value1 and value2 can have a comma(,) in their values.

I see 2 different behaviors with spring request parameter mapping.

Case 1. /path?param=part1%2Cpart2 (url encoded comma)

Spring request parameter maps this to a List of size 2 with part1 and part2 as the elements, but HttpServletRequest.getParameterValues("param") is correctly assigned to an array of size 1 with value=part1,part2

Case 2. /path?param=part1%2Cpart2&param=part3%2Cpart4

In this case Spring correctly maps this to a list of 2 values, and so does the HttpServletRequest parameter.

I guess Spring is supporting mapping List parameter with both csv values and repeating the parameter. Is there a way to tell Spring to use a particular mapping method?

I'm using spring-mvc 3.2.13

@Controller
public class MyController {

    @RequestMapping(value = "/mymethod", method = RequestMethod.POST)
    public @ResponseBody Boolean method(MyRequest myReq, HttpServletRequest request) {

    }

 }

 public class MyRequest {
    List<String> param;
 }
1
From SpringMVC 3 experience, I've never seen @Controller handle multiple keys in GET params. Post your @Controller code, then we have more information to work from. - Jan Vladimir Mostert
added the controller code. - pkrish

1 Answers

1
votes

I've actually never seen this before, but have done a bit of reading and the recommended way to do it for POST seems to be to pass the values as follow:

/path?param=value1&param=value2&param=value3

And then in the @Controller, have a @RequestParam which contains a value of param[] - note the square brackets.

@RequestMapping(value="/path", method = RequestMethod.POST)
public void doSomething(@RequestParam(value = "param[]") String[] paramValues){...}

I haven't tested POST, but I have tested GET and I got it working using:

/path?code=1&height=300&width=300&param=3&param=5&param=7

And then in my @Controller

@ResponseBody
@RequestMapping(value = "/path", method = RequestMethod.GET)
public void blah(
        HttpServletResponse response,
        @RequestParam("code") String code,
        @RequestParam("width") Integer width,
        @RequestParam("height") Integer height,
        @RequestParam(value = "param") String[] params) {

        for (int i = 0; i < params.length; i++){
            System.out.println(params[i]);
        }

And this printed

3
5
7