Spring MVC Truncating Path Variable IP Address

Ran into an interesting Spring MVC convulsion.  If you use a path variable with a “.” in it, then the variable is truncated before it gets into your code via your controller.

For instance, say you make a request like:


And your spring controller handles it with this:

@RequestMapping(method = RequestMethod.GET, value = “/path/{ipAddress}”)
public @ResponseBody SomeResponseObject handlerMethod (@PathVariable String ipAddress) {…}

The variable ipAddress arrives in as 12.4.55, not — it’s been truncated by Spring.

I’ve read through a lot of explanations, and it seems Spring considers the last part of a string with a “.” to be a file extension, and truncates it — useful for .json and .xml files.  Understandable.

Spring now has annotation settings to turn the behavior off in the configuration (as of this date, Spring version 4.0.6 is out).  I think some better solutions are:

  • Using a Regular Expression filter in the @RequestMapping i.e. @RequestMapping(method = RequestMethod.GET, value = “/path/{ipAddress:.*}”)
  •  Using a request Param instead so the url is /myapp/machine?ipAddress=
  •  Using the HttpServletresponse or WebResponse as a parameter in the method.

My favorite is the Regular Expression solution.

I’ve coded up samples in a simple Spring MVC app that runs in Maven/Java 6 or 7; the project is called “springrestparam” located here:


, ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>