views:

315

answers:

2

When experimenting with Spring MVC, I noticed the values passed to controller arguments annotated with @PathVariable will have all the characters from the last '.' on stripped, unless the last character is a '/'.

For example, given the following code:

@RequestMapping("/host/${address})"
public String getHost(@PathVariable String address, Model model) {
    model.addAttribute("host", hostRepository.getHost(address));
    return "host";
}

If the URL is "/host/127.0.0.1", the value of address will be "127.0.0". If the URL is "/host/127.0.0.1/", the value of address will be "127.0.0.1".

Is there away to prevent this stripping?

A: 

It's apparently been handled as a file extension and stripped. Not sure if it's a bug. I would fill an issue at their issuetracker.

Update: please check this topic, it's actually not a bug and it can be solved programmatically: Trying to create REST-ful URLs with mulitple dots in the “filename” part - Spring 3.0 MVC

BalusC
That was my thought, too; just wanted to see if anyone else had info.
Matt McClellan
+2  A: 

There are plenty of such reports in their issue tracker already (for example, SPR-5778). But they don't fix it, so it seems to be a legitimate behaviour.

The official workaround is to set useDefaultSuffixPattern = false on DefaultAnnotationHandlerMapping, but it has several drawbacks:

  • It is applied to all controllers
  • It completely disables extension handling (for example, for ContentNegotiationViewResolver)
  • It breaks "trailing slash doesn't matter" rule

More sophisticated workarounds use a customized PathMatcher, as described here.

axtavt