dom farr
dom farr

Reputation: 4181

Spring MVC 3 RequestMapping with regular expresssion quantifiers

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 matcher 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!

Upvotes: 3

Views: 7222

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

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.

Upvotes: 6

axtavt
axtavt

Reputation: 242686

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.

Upvotes: 5

Related Questions