Java 与“匹配”;URL的其余部分;使用Spring3请求映射注释

Java 与“匹配”;URL的其余部分;使用Spring3请求映射注释,java,spring-mvc,Java,Spring Mvc,可能重复: 在Spring 3中,是否有一种方法可以在以下url中捕获rest/of/the/url: /myapp/foo/bar/rest/of/the/url 通过使用@RequestMapping注释,如下所示: @RequestMapping(value="{appname}/{path1}/{path2}/{remainder}") public String myRequestMethod( @PathVariable("appname") String appName

可能重复:

在Spring 3中,是否有一种方法可以在以下url中捕获
rest/of/the/url

/myapp/foo/bar/rest/of/the/url
通过使用@RequestMapping注释,如下所示:

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder}")
public String myRequestMethod(
    @PathVariable("appname") String appName, 
    PathVariable("path1") String path1, 
    PathVariable("path2") String path2,
    PathVariable("remainder") String remainder)
我希望RequestMapping像这样匹配

{appname}   -> myapp
{path1}     -> foo
{path2}     -> bar
{remainder} -> rest/of/the/url 
在for RequestMapping中,有一条关于使用备用正则表达式的说明:

默认情况下,URI模板将与正则表达式[^.]*(即句点以外的任何字符)匹配,但可以通过指定另一个正则表达式来更改,如:/hotels/{hotel:\d+}

但当我使用如下的RequestMapping时,它的行为并不像预期的那样(我得到404):

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.[\\S]*}")
有人知道如何将URL的其余部分与Spring RequestMapping匹配吗?

对于上述内容,请尝试:

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.+}")

Spring在URL中停止匹配“.”的默认行为不是很直观。我不认为句点字符在路径上下文中有任何特殊意义(与说“/”、“;”或“?”)相反。

有趣的是:我也发现了这样做的必要性。我是这样解决的:

@RequestMapping(value = {"/someChildUrlIfYouWant/**"}
这里的
“**”
上写着“在我左边的任何子路径上抓取任何东西”。如果您想要实际匹配的路径到达此方法,可以使用:

request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE )
您将获得
/someChildUrlIfYouWant/the/full/path/to/whatever.html
,因此,如果您只需要变量子路径,则必须修剪字符串的前部


有意义吗?

你说得绝对正确。这是重复的。是的,但我更喜欢这个问题的措辞。嘿,这很接近我想要的,但它不像我预期的那样有效。如果我请求url someappname/apath/anotherpath/whatever.file,我将能够检索剩余的路径变量whatever.file。但是,如果我请求someappname/apath/anotherpath/和/an/other/path/whatever.file,您提供的模式(+)将不匹配,当我以为我会得到这个值和/an/other/path/whatever.file时,您或其他阅读本文的人可以建议我应该使用哪种模式来获得url余数吗?
@RequestMapping({appname}/{path1}/{path2}/{resident:.*}”)
@dgeske它不起作用。您应该使用。这是可行的。只有在剩余部分不包含任何斜杠的情况下,这才对我有效。我也这么做了,可惜没有更好的选项,因为如果更改端点的名称,它将不再有效