Skip to content

Commit 3f5fa44

Browse files
committed
Polish PathVariableMapMethodArgumentResolver
Return an empty map when there are no path variables, rather than raising an exception. This is consistent with similar resolvers for extracting headers and request parameters. Issue: SPR-9289
1 parent 4fd7645 commit 3f5fa44

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.core.MethodParameter;
2323
import org.springframework.util.CollectionUtils;
2424
import org.springframework.util.StringUtils;
25-
import org.springframework.web.bind.ServletRequestBindingException;
2625
import org.springframework.web.bind.annotation.PathVariable;
2726
import org.springframework.web.bind.support.WebDataBinderFactory;
2827
import org.springframework.web.context.request.NativeWebRequest;
@@ -31,6 +30,8 @@
3130
import org.springframework.web.method.support.ModelAndViewContainer;
3231
import org.springframework.web.servlet.HandlerMapping;
3332

33+
import edu.emory.mathcs.backport.java.util.Collections;
34+
3435
/**
3536
* Resolves {@link Map} method arguments annotated with an @{@link PathVariable}
3637
* where the annotation does not specify a path variable name. The created
@@ -49,9 +50,7 @@ public boolean supportsParameter(MethodParameter parameter) {
4950
}
5051

5152
/**
52-
* Return a Map with all URI template variables.
53-
* @throws ServletRequestBindingException if no URI vars are found in the
54-
* request attribute {@link HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE}
53+
* Return a Map with all URI template variables or an empty map.
5554
*/
5655
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
5756
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
@@ -61,12 +60,12 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
6160
(Map<String, String>) webRequest.getAttribute(
6261
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
6362

64-
if (CollectionUtils.isEmpty(uriTemplateVars)) {
65-
throw new ServletRequestBindingException(
66-
"No URI template variables for method parameter type [" + parameter.getParameterType() + "]");
63+
if (!CollectionUtils.isEmpty(uriTemplateVars)) {
64+
return new LinkedHashMap<String, String>(uriTemplateVars);
65+
}
66+
else {
67+
return Collections.emptyMap();
6768
}
68-
69-
return new LinkedHashMap<String, String>(uriTemplateVars);
7069
}
7170

7271
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertTrue;
2222

2323
import java.lang.reflect.Method;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627

@@ -89,9 +90,12 @@ public void resolveArgument() throws Exception {
8990
assertEquals(uriTemplateVars, result);
9091
}
9192

92-
@Test(expected=ServletRequestBindingException.class)
93+
@Test
94+
@SuppressWarnings("unchecked")
9395
public void resolveArgumentNoUriVars() throws Exception {
94-
resolver.resolveArgument(paramMap, mavContainer, webRequest, null);
96+
Map<String, String> map = (Map<String, String>) resolver.resolveArgument(paramMap, mavContainer, webRequest, null);
97+
98+
assertEquals(Collections.emptyMap(), map);
9599
}
96100

97101

0 commit comments

Comments
 (0)