3

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;
 }
3
  • 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. Commented Nov 2, 2015 at 19:03
  • added the controller code. Commented Nov 2, 2015 at 19:09
  • Some solutions are here stackoverflow.com/questions/4998748/… Commented Feb 24, 2022 at 21:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

The main issue is that my values contain comma. Can you try replacing param=3 with param=test,value?
This is the closest you'll come to getting lists from SpringMVC as far as I've read yesterday, if you want to make it work, you'll need to send it in the format param=test&param=value or alternatively manually split it using String[] subparams = param.split(",");
Think you misunderstood. "test,value" is 1 parameter, but spring splits it into 2. See the 2 cases above in the question.
I'm also looking for answer to this problem. when you pass just one param with "test,value" like ?param=test,value it gives you 2 values, instead it should give 1 value

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.