Trying to create REST-ful URLs with multiple dots in the “filename” part - Spring 3.0 MVC

后端 未结 7 1588
旧巷少年郎
旧巷少年郎 2020-12-03 02:11

I\'m using Spring MVC (3.0) with annotation-driven controllers. I would like to create REST-ful URLs for resources and be able to not require (but still opt

7条回答
  •  天涯浪人
    2020-12-03 02:26

    Probably, it's an ugly hack, I just wanted to explore extensibility of Spring @MVC. Here is a customized PathMatcher. It uses $ in the pattern as the end marker - if pattern ends with it, marker is removed and pattern is matched by the default matcher, but if pattern has $ in the middle (e.g. ...$.*), such a pattern is not matched.

    public class CustomPathMatcher implements PathMatcher {
        private PathMatcher target;
    
        public CustomPathMatcher() {
            target = new AntPathMatcher();
        }
    
        public String combine(String pattern1, String pattern2) {
            return target.combine(pattern1, pattern2); 
        }
    
        public String extractPathWithinPattern(String pattern, String path) {
            if (isEncoded(pattern)) {
                pattern = resolvePattern(pattern);
                if (pattern == null) return "";
            }
            return target.extractPathWithinPattern(pattern, path);
        }
    
        public Map extractUriTemplateVariables(String pattern,
                String path) {
            if (isEncoded(pattern)) {
                pattern = resolvePattern(pattern);
                if (pattern == null) return Collections.emptyMap();
            }
            return target.extractUriTemplateVariables(pattern, path);
        }
    
        public Comparator getPatternComparator(String pattern) {
            final Comparator targetComparator = target.getPatternComparator(pattern);
            return new Comparator() {
                public int compare(String o1, String o2) {
                    if (isEncoded(o1)) {
                        if (isEncoded(o2)) {
                            return 0;
                        } else {
                            return -1;
                        }
                    } else if (isEncoded(o2)) {
                        return 1;
                    }
                    return targetComparator.compare(o1, o2);
                }        
            };
        }
    
        public boolean isPattern(String pattern) {
            if (isEncoded(pattern)) {
                pattern = resolvePattern(pattern);
                if (pattern == null) return true;
            }
            return target.isPattern(pattern);
        }
    
        public boolean match(String pattern, String path) {
            if (isEncoded(pattern)) {
                pattern = resolvePattern(pattern);
                if (pattern == null) return false;
            }
            return target.match(pattern, path);
        }
    
        public boolean matchStart(String pattern, String path) {
            if (isEncoded(pattern)) {
                pattern = resolvePattern(pattern);
                if (pattern == null) return false;
            }
            return target.match(pattern, path);
        }
    
        private boolean isEncoded(String pattern) {
            return pattern != null && pattern.contains("$");
        }
    
        private String resolvePattern(String pattern) {
            int i = pattern.indexOf('$');
            if (i < 0) return pattern;
            else if (i == pattern.length() - 1) {
                return pattern.substring(0, i);
            } else {
                String tail = pattern.substring(i + 1);
                if (tail.startsWith(".")) return null;
                else return pattern.substring(0, i) + tail;
            }
        }
    }
    

    Config:

    
    
    
        
    
    
    
        
    
    

    And usage (given "/hello/1.2.3", value is "1.2.3"):

    @RequestMapping(value = "/hello/{value}$", method = RequestMethod.GET)
    public String hello(@PathVariable("value") String value, ModelMap model)
    

    EDIT:: Now doesn't break "trailing slash doesn't matter" rule

提交回复
热议问题