@RequestMapping正则表达式

Pav*_*nko 3 spring spring-mvc

我正在尝试为spring @RequestMapping注释创建值属性,以便像这样映射url

/educationDistrict/308/action/resetAddressesForYear/1
Run Code Online (Sandbox Code Playgroud)

还有这个

/educationDistrict/308/action/resetAddressesForYear
Run Code Online (Sandbox Code Playgroud)

我有这个

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)

但第一个网址不匹配.

我不能使用多重值因为春天hateoas https://github.com/spring-projects/spring-hateoas/issues/186

春天4.1.5

Mit*_*hun 5

/**在URL映射的末尾添加@RequestMapping.您可以检索URL的最后部分,如下所示:

@RequestMapping(value = "/{repository}/{id}/action/{methodName:[A-z]*}{v:.*}/**", method = RequestMethod.GET)
public ModelAndView welcome(@PathVariable("methodName") String name, HttpServletRequest request) {

    String mvcPath = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    int index = StringUtils.lastIndexOf(mvcPath, "/");

    System.out.println("Method name - " + name);        
    System.out.println("Rest of the URL - " + mvcPath.substring(index+1));

    ModelAndView model = new ModelAndView();
    model.setViewName("index");
    model.addObject("name", mvcPath);

    return model;
}
Run Code Online (Sandbox Code Playgroud)

注意:我使用StringUtilsApache Commons查找最后一个索引/.