弹簧控制器中的正则表达式 [英] Regex in spring controller

查看:109
本文介绍了弹簧控制器中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个请求过滤器,只有在匹配字母e的模式,然后是数字时才会使用。但是我似乎无法让它发挥作用。每次尝试使用正则表达式时,我都会遇到400错误。

I am trying to build a request filter that will only get used if it matches a pattern of the letter e, then a number. However I cannot seem to get it to work. I keep getting 400 errors every time I try something with regex.

如果我只是使用以下内容它有效,但也可以捕获没有数字的映射。不想要。

If I just use the following it "works" but also captures mappings that do not have numbers which I don't want.

@RequestMapping(value = "e{number}",
            method = RequestMethod.GET)

我尝试了以下组合。

@RequestMapping(value = "e{number}",
            params = "number:\\d+",
            method = RequestMethod.GET)

@RequestMapping(value = "e{number:\d+}",
            method = RequestMethod.GET)

@RequestMapping(value = "/e{^\\+?\\d+\$}",
            method = RequestMethod.GET)

@RequestMapping(value = "/{^\\e+?\\d+\$}",
            method = RequestMethod.GET)


推荐答案

根据文档,你必须使用像 {var这样的东西名称:正则表达式} 。还有一个例子:

According to the documentation, you have to use something like {varName:regex}. There's even an example :

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
  public void handle(@PathVariable String version, @PathVariable String extension) {
    // ...
  }
}

这篇关于弹簧控制器中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆