0

I wrote a spring-mvc controller method to get an array of values in the request parameter.The method looks like below

/**
 Trying to get the value for request param foo which passes multiple values

**/
@RequestMapping(method=RequestMethod.GET)
public void performActionXX(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam("foo") String[] foo) {

......
......

}

The above method works fine when the request url is in below format

...?foo=1234&foo=0987&foo=5674.

However when the request url is in below format the server returns 400 error

...?foo[0]=1234&foo[1]=0987&foo[2]=5674

Any idea how to fix the method to cater to the second format request url?

2
  • why do you want to do it this way ? "foo[0]=1234&foo[1]=0987&foo[2]=5674" and not the other way? any special reason? Commented Nov 26, 2013 at 22:16
  • I don't have the control over the request url Commented Dec 4, 2013 at 19:24

2 Answers 2

1

This is not possible with @RequestParam. What you can do is implement and register your own HandlerMethodArgumentResolver to perform to resolve request parameters like

...?foo[0]=1234&foo[1]=0987&foo[2]=5674

into an array. You can always checkout the code of RequestParamMethodArgumentResolver to see how Spring does it.

Note that I recommend you change how the client creates the URL. The server is supposed to define an API and the client is meant to follow it, that's why we have the 400 Bad Request status code.

Sign up to request clarification or add additional context in comments.

Comments

0

I resolved this issue using the request.getParameterMap().Below is code.

Map<String,String> parameterMap=  request.getParameterMap();
    for(String key :parameterMap.keySet()){
        if(key.startsWith("nameEntry")){
            nameEntryLst.add(request.getParameter(key));
        }
    }

Comments

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.