Trying to create REST-ful URLs with multiple dots in the “filename” part - Spring 3.0 MVC

后端 未结 7 1548
旧巷少年郎
旧巷少年郎 2020-12-03 02:11

I\'m using Spring MVC (3.0) with annotation-driven controllers. I would like to create REST-ful URLs for resources and be able to not require (but still opt

相关标签:
7条回答
  • 2020-12-03 02:37

    I had the same problem and I also solved it by custom PathMatcher. My solution is somewhat simplier to what axtavt proposed. My PathMatcher also has a private final AntPathMatcher target, and it delegates all calls to it unchanged, except for match() method:

    @Override
    public boolean match(String pattern, String path) {
        return pattern.endsWith(".*") ? false : target.match(pattern, path);
    }
    

    This works because Spring tries to match controllers by adding "." to the end. For example, with path mapping "/widgets/{id}" and URL "/widgets/1.2.3.4", Spring tries first match againts "/widgets/{id}." and after that "/widgets/{id}". The first will match, but it leaves only "1.2.3" for id.

    My PatchMatcher specifically rejects patterns ending ".*", thus the first attempt fails and the second matches.

    If you are using ContentNegotiatingViewResolver you can still specify content type in URL's using request parameter "format" (if the favorParameter is set to true).

    -jarppe

    0 讨论(0)
提交回复
热议问题