{"id":1194,"date":"2014-08-27T14:28:29","date_gmt":"2014-08-27T21:28:29","guid":{"rendered":"http:\/\/10kdev.net\/?p=1194"},"modified":"2014-08-28T09:40:20","modified_gmt":"2014-08-28T16:40:20","slug":"spring-mvc-truncating-path-variable-ip-address","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1194","title":{"rendered":"Spring MVC Truncating Path Variable IP Address"},"content":{"rendered":"<p>Ran into an interesting Spring MVC convulsion. \u00a0If you use a path variable with a &#8220;.&#8221; in it, then the variable is truncated before it gets into your code via your controller.<\/p>\n<p>For instance, say you make a request like:<\/p>\n<p><strong>\/myapp\/machine\/12.4.55.137<\/strong><\/p>\n<p>And your spring controller handles it with this:<\/p>\n<p><strong>@RequestMapping(method = RequestMethod.GET, value = &#8220;\/path\/{ipAddress}&#8221;)<br \/>\n<strong>public @ResponseBody SomeResponseObject\u00a0handlerMethod\u00a0(@PathVariable String ipAddress) {&#8230;}<\/strong><\/strong><\/p>\n<p>The variable <em>ipAddress<\/em> arrives in as <em><strong>12.4.55, not 12.4.55.137<\/strong><\/em> &#8212; it&#8217;s been truncated by Spring.<\/p>\n<p>I&#8217;ve read through a lot of explanations, and it seems Spring considers the last part of a string with a &#8220;.&#8221; to be a file extension, and truncates it &#8212; useful\u00a0for .json and .xml files. \u00a0Understandable.<\/p>\n<p>Spring now has annotation settings to turn the behavior off in the configuration (as of this date, Spring version 4.0.6 is out). \u00a0I think some better solutions are:<\/p>\n<ul>\n<li>Using a Regular Expression\u00a0filter in the @RequestMapping i.e.\u00a0<strong>@RequestMapping(method = RequestMethod.GET, value = &#8220;\/path\/{ipAddress:.*}&#8221;)<\/strong><\/li>\n<li>\u00a0Using a request Param instead so the url is\u00a0<strong>\/myapp\/machine?ipAddress=12.4.55.137<\/strong><\/li>\n<li>\u00a0Using the HttpServletresponse or WebResponse as a parameter in the method.<\/li>\n<\/ul>\n<p>My favorite is the Regular Expression solution.<\/p>\n<p>I&#8217;ve coded up samples in a simple Spring MVC app that runs in Maven\/Java 6 or 7; the project is called &#8220;<em><strong>springrestparam<\/strong><\/em>&#8221; located here:<\/p>\n<p style=\"padding-left: 30px;\">https:\/\/bitbucket.org\/bilmowry\/10kdev\/src<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ran into an interesting Spring MVC convulsion. \u00a0If you use a path variable with a &#8220;.&#8221; in it, then the variable is truncated before it gets into your code via your controller. For instance, say you make a request like: \/myapp\/machine\/12.4.55.137 And your spring controller handles it with this: @RequestMapping(method = RequestMethod.GET, value = &#8220;\/path\/{ipAddress}&#8221;) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[29,30,28],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1194"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1194"}],"version-history":[{"count":3,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1194\/revisions"}],"predecessor-version":[{"id":1197,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1194\/revisions\/1197"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}