- Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Affects: 2.5.2
Many http clients pass an array to queryString in this form:
?attributes[]=field1&attributes[]=field2
I'm trying to get Controller to work with this format through RequestParam:
@RestController public class FooController { @RequestMapping(method = RequestMethod.GET, value = "/foo", produces = "application/json") ResponseEntity<FooDto> getFoo(@RequestParam(value = "attributes", required = false) List<String> attributes) { } } However, when executing a request, null is passed to the controller method as the attributes value.
I found that the "RequestParamMethodArgumentResolver.resolveName" method receives "attributes" as the value of the parameter name, while this parameter is stored in the request object using the key "attributes[]". Therefore, the argument of the controller method is null, because resolver cannot find a match.
How would you like to add such support to RequestParamMethodArgumentResolver? The code could look something like this:
@Override protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { if ( ( parameterTypeIs(List.class, parameter) || parameterTypeIs(Set.class, parameter) ) && nameIsIndexParameter(name, request)) { return super.resolveName(name + "[]", parameter, request); } return super.resolveName(name, parameter, request); } private boolean parameterTypeIs(Class<?> parameterType, MethodParameter parameter) { return parameterType.isAssignableFrom(parameter.getParameterType()); } private boolean nameIsIndexParameter(String name, NativeWebRequest request) { return request.getParameterMap().containsKey(name + "[]"); } In my project, I replaced the built-in RequestParamMethodArgumentResolver with a custom one. An example can be found in the description of the question on stackoverflow