1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet.mvc.method.annotation;
18
19 import java.util.Collections;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22
23 import org.springframework.core.MethodParameter;
24 import org.springframework.util.CollectionUtils;
25 import org.springframework.util.StringUtils;
26 import org.springframework.web.bind.annotation.PathVariable;
27 import org.springframework.web.bind.support.WebDataBinderFactory;
28 import org.springframework.web.context.request.NativeWebRequest;
29 import org.springframework.web.context.request.RequestAttributes;
30 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
31 import org.springframework.web.method.support.ModelAndViewContainer;
32 import org.springframework.web.servlet.HandlerMapping;
33
34
35
36
37
38
39
40
41
42
43 public class PathVariableMapMethodArgumentResolver implements HandlerMethodArgumentResolver {
44
45 @Override
46 public boolean supportsParameter(MethodParameter parameter) {
47 PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
48 return (ann != null && (Map.class.isAssignableFrom(parameter.getParameterType()))
49 && !StringUtils.hasText(ann.value()));
50 }
51
52
53
54
55 @Override
56 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
57 NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
58
59 @SuppressWarnings("unchecked")
60 Map<String, String> uriTemplateVars =
61 (Map<String, String>) webRequest.getAttribute(
62 HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
63
64 if (!CollectionUtils.isEmpty(uriTemplateVars)) {
65 return new LinkedHashMap<String, String>(uriTemplateVars);
66 }
67 else {
68 return Collections.emptyMap();
69 }
70 }
71
72 }