Search code examples
springspring-mvcrequest-mapping

Spring 3 RequestMapping: Get path value


Is there a way to get the complete path value after the requestMapping @PathVariable values have been parsed?

That is: /{id}/{restOfTheUrl} should be able to parse /1/dir1/dir2/file.html into id=1 and restOfTheUrl=/dir1/dir2/file.html

Any ideas would be appreciated.


Solution

  • Non-matched part of the URL is exposed as a request attribute named HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE:

    @RequestMapping("/{id}/**")
    public void foo(@PathVariable("id") int id, HttpServletRequest request) {
        String restOfTheUrl = new AntPathMatcher().extractPathWithinPattern(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(),request.getRequestURI());
        ...
    }