开发者

Spring MVC 3 RequestMapping with regular expresssion quantifiers

开发者 https://www.devze.com 2023-02-11 17:31 出处:网络
The method below fails with \"PatternSyntaxException: Unclosed counted closure near index ... \" @RequestMapping(value =\"/{id:[0-9|a-z]{15}}\")

The method below fails with "PatternSyntaxException: Unclosed counted closure near index ... "

@RequestMapping(value ="/{id:[0-9|a-z]{15}}")
public View view(@PathVariable final String id) {
  ...
}

Looks like the pattern mat开发者_运维百科cher is trimming too much off the the string and losing the last }.

Does anyone know a work around to this bug? I'm having to drop the qualifier to "/{id:[0-9|a-z]+}" - which frankly suck!


Here's a solution. It's butt-ugly, but it's equivalent to what you'd like to have:

@RequestMapping(value = "/{id:[0-9a-z][0-9a-z][0-9a-z][0-9a-z]" +
        "[0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z]" +
        "[0-9a-z][0-9a-z][0-9a-z][0-9a-z]}") // 15 repetitions of [0-9a-z]

If that's the only way to get what you need, you might as well use this monster.


I don't think there are any good workarounds for this case except for manual validation. After all, {name:regexp} syntax was introduced for solving ambiguities between mappings rather than for validation.

@Valid on @PathVariables could be a solution, but it's promised only in Spring 3.1 (SPR-6380).

Also feel free to report this bug in Spring JIRA, although I don't expect them to fix it quickly since path variable handling code is already a mess.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号