Spring @PathVariable mapping incomplete path when dots are included

In all my projects this happens at least once, and I’m surprised over and over again: a controller will not map just anything to @PathVariable by default. Contrary to intuition, the annotation’s argument is a regular expression which excludes some characters per default. For instance the url

http://localhost:8080/myapp/api/user/testuser@example

when mapped to a controller:

@RequestMapping(value = "/api/user/{email}")
ModelAndView findUserByEmail(@PathVariable("email") String email){

// email = testuser@example

 
}

will result in “testuser@example” … missing the “.com” suffix.

The correct mapping is:

@RequestMapping(value = "/api/user/{email:.*}")
ModelAndView findUserByEmail(@PathVariable("email") String email){

// email = testuser@example.com

 
}

More here [1]

Resources

[1] Problems with @RequestMapping
http://forum.springsource.org/showthread.php?78085-Problems-with-RequestMapping&p=263563

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.