22

Possible Duplicate:
Spring 3 RequestMapping: Get path value

In Spring 3, is there a way to capture rest/of/the/url in the following URL:

/myapp/foo/bar/rest/of/the/url

by using a @RequestMapping annotation like this:

@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)

I would like the RequestMapping to match like this

{appname}   -> myapp
{path1}     -> foo
{path2}     -> bar
{remainder} -> rest/of/the/url 

In the Javadocs for RequestMapping there is a note about using an alternate regular expression:

By default, the URI template will match against the regular expression [^.]* (i.e. any character other than period), but this can be changed by specifying another regular expression, like so: /hotels/{hotel:\d+}

But this doesn't behave as expected (I get 404) when I use a RequestMapping like so:

@RequestMapping(value="{appname}/{path1}/{path2}/{remainder:.[\\S]*}")

Does anyone know how to match the rest of an URL with a Spring RequestMapping?

Community
  • 1
  • 1
David Krisch
  • 403
  • 1
  • 5
  • 12

2 Answers2

47

Funny thing: I just came across the need to do this too. Here's how I solved it:

@RequestMapping(value = {"/someChildUrlIfYouWant/**"}

The "**" here says 'grab anything at any sub-path of what's to the left of me.' If you want the path it actually matched to get to this method, you can use:

request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE )

You will get /someChildUrlIfYouWant/the/full/path/to/whatever.html so if you just want the variable child path, you'll have to trim the front part of the string.

Make sense?

lschin
  • 6,745
  • 2
  • 38
  • 52
vinnybad
  • 2,102
  • 3
  • 19
  • 29
  • 1
    I did same, too bad there is not a better options because if you change the name of your endpoint, it won't work anymore – Luc DUZAN May 22 '15 at 08:31
8

For the above try:

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

The default behavior of Spring to stop matching on '.' in urls is not very intuitive. I don't think period character has any special meaning in the context of the path(as opposed to say '/', ';' or '?').

rjc730
  • 125
  • 2
  • 4
  • 1
    Hey, this is so close to what I want, but it does not work as I would expect. If I were to request the url someappname/apath/anotherpath/whatever.file I *would* be able to retrieve the remainder path variable as whatever.file However, if I were to request someappname/apath/anotherpath/and/an/other/path/whatever.file the pattern you supplied (.+) would not match, when I would have thought I would have got the value and/an/other/path/whatever.file Can you or somebody else reading this suggest which pattern I should use to get the url remainder? – VLostBoy Sep 15 '11 at 14:24
  • `@RequestMapping("{appname}/{path1}/{path2}/{remainder:.*}")` – dgeske May 02 '12 at 16:37
  • @dgeske It doesn't work. – uthark Dec 06 '12 at 23:00
  • You should use http://stackoverflow.com/a/11248730/190713. That works. – vinnybad Oct 29 '13 at 14:37
  • 1
    This only worked for me if the remainder did not contain any slashes. – BillMan Feb 10 '16 at 18:27