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?
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 = falseonDefaultAnnotationHandlerMapping, but it has several drawbacks:ContentNegotiationViewResolver)More sophisticated workarounds use a customized
PathMatcher, as described here.