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
This commit is contained in:
@@ -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<String, String>) 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<String, String>(uriTemplateVars);
|
||||
}
|
||||
else {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return new LinkedHashMap<String, String>(uriTemplateVars);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user