如何在RequestMapping中使用正则表达式路径映射器变量?

时间:2013-07-26 19:36:52

标签: spring spring-mvc

我正在尝试使用请求映射器来匹配这样的网址:

http://localhost:9999/si/configs/some/path/on/the/server/file.xml

我有一个像这样设置的控制器,但RequestMapping方法的findConfig的值不正确。这是我的代码(剥离了一切无关的东西):

@Controller
@RequestMapping("/si/configs")
public class SIController {

    @RequestMapping(value = "{path:/**/*.xml}", method = RequestMethod.GET)
    public ResponseEntity<String> findConfig(@PathVariable("path") String path) {
        // value of path arg should be: "/some/path/on/the/server/file.xml"
   }
}

我知道Spring在路径匹配字符串中允许使用正则表达式,我试图遵循这个问题的建议:Spring URI Template Patterns with Regular Expressions,但我似乎无法理解这一点。

请求映射应该包含哪些内容?


修改

当我删除路径变量并执行此操作时:

    @RequestMapping(value = "/**/*.xml", method = RequestMethod.GET)
    public ResponseEntity<String> findConfig() {
    }

我的控制器方法按预期调用。因此,有一些关于尝试在路径变量上匹配春天不喜欢的野生植物。

1 个答案:

答案 0 :(得分:1)

如何直接使用HttpServletRequest?

@RequestMapping(value = "/**/*.xml", method = RequestMethod.GET)
public ResponseEntity<String> findConfig(HttpServletRequest request) {
    String path = request.getServletPath()/getRequestURL()/getPathInfo()
    //I forget which one could give the correct part, maybe some parse operation needed

}