Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringMVC中匹配URL部分?_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

如何在SpringMVC中匹配URL部分?

如何在SpringMVC中匹配URL部分?,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,有没有办法计算Spring控制器中剩余的URL部分 i、 e.如果我的路径是/user/{userId}/*,我可以获取userId参数和url的其余部分吗?那一部分 例如对于/user/1/this/is/a/path.html?a=b我应该得到 userId=1和userUrl=/this/is/a/path.html?a=b 我见过一些解决方案,也在谷歌上搜索过一些,但它们看起来有点奇怪(很可能是因为答案是针对较旧版本的Spring),所以在较新版本中,如何以干净的方式实现这一点?是的,下

有没有办法计算Spring控制器中剩余的URL部分

i、 e.如果我的路径是
/user/{userId}/*
,我可以获取userId参数和url的其余部分吗?那一部分

例如对于
/user/1/this/is/a/path.html?a=b
我应该得到
userId=1
userUrl=/this/is/a/path.html?a=b


我见过一些解决方案,也在谷歌上搜索过一些,但它们看起来有点奇怪(很可能是因为答案是针对较旧版本的Spring),所以在较新版本中,如何以干净的方式实现这一点?

是的,下面是一个根据答案改编的示例

在本例中,
userId=1
finalPath=this/is/a/path.html

@GetMapping("/user/{userId}/**")
    public void get(@PathVariable("userId") String userId, HttpServletRequest request){
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String  patternMatch = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        AntPathMatcher apm = new AntPathMatcher();
        String finalPath = apm.extractPathWithinPattern(patternMatch, path);
    }