Browse Source

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
pull/101/merge
Rossen Stoyanchev 14 years ago
parent
commit
3f5fa44d32
  1. 17
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolver.java
  2. 8
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMapMethodArgumentResolverTests.java

17
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.core.MethodParameter;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest; 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.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import edu.emory.mathcs.backport.java.util.Collections;
/** /**
* Resolves {@link Map} method arguments annotated with an @{@link PathVariable} * Resolves {@link Map} method arguments annotated with an @{@link PathVariable}
* where the annotation does not specify a path variable name. The created * 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. * Return a Map with all URI template variables or an empty map.
* @throws ServletRequestBindingException if no URI vars are found in the
* request attribute {@link HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE}
*/ */
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
@ -61,12 +60,12 @@ public class PathVariableMapMethodArgumentResolver implements HandlerMethodArgum
(Map<String, String>) webRequest.getAttribute( (Map<String, String>) webRequest.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
if (CollectionUtils.isEmpty(uriTemplateVars)) { if (!CollectionUtils.isEmpty(uriTemplateVars)) {
throw new ServletRequestBindingException(
"No URI template variables for method parameter type [" + parameter.getParameterType() + "]");
}
return new LinkedHashMap<String, String>(uriTemplateVars); return new LinkedHashMap<String, String>(uriTemplateVars);
} }
else {
return Collections.emptyMap();
}
}
} }

8
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 static org.junit.Assert.assertTrue;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -89,9 +90,12 @@ public class PathVariableMapMethodArgumentResolverTests {
assertEquals(uriTemplateVars, result); assertEquals(uriTemplateVars, result);
} }
@Test(expected=ServletRequestBindingException.class) @Test
@SuppressWarnings("unchecked")
public void resolveArgumentNoUriVars() throws Exception { public void resolveArgumentNoUriVars() throws Exception {
resolver.resolveArgument(paramMap, mavContainer, webRequest, null); Map<String, String> map = (Map<String, String>) resolver.resolveArgument(paramMap, mavContainer, webRequest, null);
assertEquals(Collections.emptyMap(), map);
} }

Loading…
Cancel
Save