3

I want to match URLs of the form http://host/10.39284/LKJF283/23332/dd (where the path will always start with 10. and the rest will be a mix of dots, slashes, letters, and numbers) and store the entire thing after and including the 10. into a PathVariable.

I was thinking I could do this with a regular expression like so:

@RequestMapping(value="/{key:10\.+}", method=RequestMethod.GET)
    public String summary(@PathVariable String key, Model model) {
}

But this gives me an error saying "Invalid escape sequence". Any idea how I can accomplish this?

Eddie
  • 919
  • 2
  • 9
  • 21
  • If slashes won't be escaped it won't work anyway. – Grzegorz Rożniecki Sep 20 '12 at 16:35
  • Dot is being interpreted by Spring as file extension. You need to disable this - see http://stackoverflow.com/questions/2079299/trying-to-create-rest-ful-urls-with-mulitple-dots-in-the-filename-part-sprin – nickdos Sep 20 '12 at 21:07

1 Answers1

6

This is how I got it working. As far as I can tell Spring cannot handle slashes in the URL that are not intended to be path separators. So, I instead user the url rewrite filter found here: http://www.tuckey.org/urlrewrite/.

I enabled it in my web.xml

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Then added this to my WEB-INF/urlrewrite.xml

<urlrewrite>
    <rule>
       <from>^/(10\..*)$</from>
       <to>/keysummary?key=$1</to>
    </rule>
</urlrewrite>

And wrote my controller like so

@RequestMapping(value="/keysummary", method=RequestMethod.GET)
public String DOISummary(@RequestParam("key") String key, Model model) {
}
Eddie
  • 919
  • 2
  • 9
  • 21