@RequestMapping 正则表达式

3

我正在尝试使用Spring的@RequestMapping注解为值属性创建映射URL,如下所示:

/educationDistrict/308/action/resetAddressesForYear/1

还有这个

/educationDistrict/308/action/resetAddressesForYear

我需要这个

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

但是第一个URL不匹配。

由于Spring HATEOAS,我不能使用多个值。 https://github.com/spring-projects/spring-hateoas/issues/186

Spring 4.1.5

1个回答

5
@RequestMapping的URL映射结尾处添加/**。您可以按照以下方式检索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;
}

注意: 我使用了StringUtils Apache Commons来查找最后一个/的索引。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接