diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java index b0a24ced35..b1093f4d4a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java @@ -22,7 +22,6 @@ import java.util.Map; import org.springframework.core.MethodParameter; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; @@ -31,6 +30,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.servlet.HandlerMapping; +import edu.emory.mathcs.backport.java.util.Collections; + /** * Resolves {@link Map} method arguments annotated with an @{@link PathVariable} * where the annotation does not specify a path variable name. The created @@ -49,9 +50,7 @@ public class PathVariableMapMethodArgumentResolver implements HandlerMethodArgum } /** - * Return a Map with all URI template variables. - * @throws ServletRequestBindingException if no URI vars are found in the - * request attribute {@link HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE} + * Return a Map with all URI template variables or an empty map. */ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { @@ -61,12 +60,12 @@ public class PathVariableMapMethodArgumentResolver implements HandlerMethodArgum (Map) webRequest.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); - if (CollectionUtils.isEmpty(uriTemplateVars)) { - throw new ServletRequestBindingException( - "No URI template variables for method parameter type [" + parameter.getParameterType() + "]"); + if (!CollectionUtils.isEmpty(uriTemplateVars)) { + return new LinkedHashMap(uriTemplateVars); + } + else { + return Collections.emptyMap(); } - - return new LinkedHashMap(uriTemplateVars); } } \ No newline at end of file diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java index 415be1c80b..5f703cf26a 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.lang.reflect.Method; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -89,9 +90,12 @@ public class PathVariableMapMethodArgumentResolverTests { assertEquals(uriTemplateVars, result); } - @Test(expected=ServletRequestBindingException.class) + @Test + @SuppressWarnings("unchecked") public void resolveArgumentNoUriVars() throws Exception { - resolver.resolveArgument(paramMap, mavContainer, webRequest, null); + Map map = (Map) resolver.resolveArgument(paramMap, mavContainer, webRequest, null); + + assertEquals(Collections.emptyMap(), map); }