仅获取@Requestmapping的通配符部分


问题内容

我在控制器操作上使用了通配符映射,并且想让通配符匹配字符串。我该如何正确做呢?不使用子串等

@Controller
@RequestMapping(value = "/properties")
public class PropertiesController {

    @RequestMapping(value = "/**", method=RequestMethod.GET)
    public void getFoo() {

        String key = (String) request. getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        // returns "/properties/foo/bar" ... 
        // I want only "foo/bar" part


        // ...
    }

}|

问题答案:

看来问题已经在这里解决了:Spring 3RequestMapping:尽管接受的答案不能回答问题,但仍获取路径值。一个建议的解决方案(这个解决方案)似乎有效,因为我刚刚尝试过:

@Controller
@RequestMapping(value = "/properties")
public class PropertiesController {

    @RequestMapping(value = "/**", method=RequestMethod.GET)
    public void getFoo(final HttpServletRequest request) {

        String path = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String bestMatchPattern = (String ) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

        AntPathMatcher apm = new AntPathMatcher();
        String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);
        // on "/properties/foo/bar", finalPath contains "foo/bar"
    }

}

我建议您对未接受的答案进行投票,即使要达到可接受的答案的分数还有很长的路要走。从在另一个线程上接受答案的时间到现在(因为不可能有一个错误的答案达到如此高的分数)之间的规格发生了变化,我不会感到惊讶。