<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=lhaemmerle" rel="nofollow">Lukas Hämmerle</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7919?redirect=false" rel="nofollow">SPR-7919</a></strong> and commented</p>
<p dir="auto">I have defined the following request mapping:<br>
<code class="notranslate">@RequestMapping</code>(value = "/services/{serviceKey}/vos", method = RequestMethod.GET)</p>
<p dir="auto">There is a problem if the value of {serviceKey} contains a "/", for example if {serviceKey}=<a href="http://example.com/foo" rel="nofollow">http://example.com/foo</a><br>
This makes sense of course since the requestmapping cannot easily know that the / is part of a PathVariable.</p>
<p dir="auto">However, I would have assumed that if the URL is URL-encoded as "http%3A%2F%2Fexample.com%2Ffoo" this should not be a problem. But apparently this values for some reason cannot mapped properly because it reports:</p>
<p dir="auto">"org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/core/services/http://example.com/foo/vos]"</p>
<p dir="auto">If the already URL-encoded values is URL-encoded again to "http%253A%252F%252Fexample.com%252Ffoo", the mapping works:</p>
<p dir="auto">"org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/core/services/http%3A%2F%2Fexample.com%2Ffoo/vos]"</p>
<p dir="auto">So, my question is whether there is a way how to make Spring accept the URL encoded value "http%3A%2F%2Fexample.com%2Ffoo" as PathVariable or whether this is a bug.</p>
<p dir="auto">I tested this on Tomcat 6 and Jetty 7.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.3</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398162940" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/15727" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/15727/hovercard" href="https://github.com/spring-projects/spring-framework/issues/15727">#15727</a> URLs containing %2F (forward slash) are not mapped correctly to <code class="notranslate">@RequestMapping</code> methods</li>
</ul>
<p dir="auto">1 votes, 3 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=rodion" rel="nofollow">Rodion</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-11101?redirect=false" rel="nofollow">SPR-11101</a></strong> and commented</p>
<p dir="auto">Controller method:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@RequestMapping(value = &quot;/test_url_decoding/{param1}&quot;)
public String[] testUrlDecoding(@PathVariable(&quot;param1&quot;) String p1)"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s1">value</span> = <span class="pl-s">"/test_url_decoding/{param1}"</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span>[] <span class="pl-s1">testUrlDecoding</span>(<span class="pl-c1">@</span><span class="pl-c1">PathVariable</span>(<span class="pl-s">"param1"</span>) <span class="pl-smi">String</span> <span class="pl-s1">p1</span>)</pre></div>
<p dir="auto">Call:<br>
/test_url_decoding/a%2Fb</p>
<p dir="auto">Expecting result: parameter p1 is set to "a/b".<br>
Current result: 404 Not found.</p>
<p dir="auto">Simular problem:<br>
<a href="http://stackoverflow.com/questions/13482020/encoded-slash-2f-with-spring-requestmapping-path-param-gives-http-400" rel="nofollow">http://stackoverflow.com/questions/13482020/encoded-slash-2f-with-spring-requestmapping-path-param-gives-http-400</a></p>
<p dir="auto">The problem was already discussed in <a href="https://jira.springsource.org/browse/SPR-7919" rel="nofollow">https://jira.springsource.org/browse/SPR-7919</a></p>
<p dir="auto">Suggested solution (setting decodeUrl of the HandlerMapping to false) does not work. The UrlPathHelper uses HttpServletRequest.getServletPath() method internally, which decodes the path.</p>
<p dir="auto">Suggested implementation for non-decoding helper:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="public class UrlPathHelperNonDecoding extends UrlPathHelper {

	public UrlPathHelperFixed() {
		super.setUrlDecode(false);
	}
	
	@Override
	public void setUrlDecode(boolean urlDecode) {
		if (urlDecode) {
			throw new IllegalArgumentException(&quot;Handler does not support URL decoding.&quot;);
		}
	}

	@Override
	public String getServletPath(HttpServletRequest request) {
		String servletPath = getOriginatingServletPath(request);
		return servletPath;
	}
	

	@Override
	public String getOriginatingServletPath(HttpServletRequest request) {
		String servletPath = request.getRequestURI().substring(request.getContextPath().length());
		return servletPath;
	}
}"><pre class="notranslate"><span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-smi">UrlPathHelperNonDecoding</span> <span class="pl-k">extends</span> <span class="pl-smi">UrlPathHelper</span> {

	<span class="pl-k">public</span> <span class="pl-smi">UrlPathHelperFixed</span>() {
		<span class="pl-en">super</span>.<span class="pl-en">setUrlDecode</span>(<span class="pl-c1">false</span>);
	}
	
	<span class="pl-c1">@</span><span class="pl-c1">Override</span>
	<span class="pl-k">public</span> <span class="pl-smi">void</span> <span class="pl-en">setUrlDecode</span>(<span class="pl-smi">boolean</span> <span class="pl-s1">urlDecode</span>) {
		<span class="pl-k">if</span> (<span class="pl-s1">urlDecode</span>) {
			<span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">IllegalArgumentException</span>(<span class="pl-s">"Handler does not support URL decoding."</span>);
		}
	}

	<span class="pl-c1">@</span><span class="pl-c1">Override</span>
	<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getServletPath</span>(<span class="pl-smi">HttpServletRequest</span> <span class="pl-s1">request</span>) {
		<span class="pl-smi">String</span> <span class="pl-s1">servletPath</span> = <span class="pl-en">getOriginatingServletPath</span>(<span class="pl-s1">request</span>);
		<span class="pl-k">return</span> <span class="pl-s1">servletPath</span>;
	}
	

	<span class="pl-c1">@</span><span class="pl-c1">Override</span>
	<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-en">getOriginatingServletPath</span>(<span class="pl-smi">HttpServletRequest</span> <span class="pl-s1">request</span>) {
		<span class="pl-smi">String</span> <span class="pl-s1">servletPath</span> = <span class="pl-s1">request</span>.<span class="pl-en">getRequestURI</span>().<span class="pl-en">substring</span>(<span class="pl-s1">request</span>.<span class="pl-en">getContextPath</span>().<span class="pl-en">length</span>());
		<span class="pl-k">return</span> <span class="pl-s1">servletPath</span>;
	}
}</pre></div>
<p dir="auto">context.xml</p>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping&quot;&gt;
  &lt;property name=&quot;order&quot; value=&quot;-1&quot;&gt;&lt;/property&gt;
  &lt;property name=&quot;urlPathHelper&quot;&gt;
    &lt;bean class=&quot;com.yoochoose.frontend.spring.UrlPathHelperFixed&quot;/&gt;
  &lt;/property&gt;
&lt;/bean&gt;"><pre class="notranslate">&lt;<span class="pl-ent">bean</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping<span class="pl-pds">"</span></span>&gt;
  &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s"><span class="pl-pds">"</span>order<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s"><span class="pl-pds">"</span>-1<span class="pl-pds">"</span></span>&gt;&lt;/<span class="pl-ent">property</span>&gt;
  &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s"><span class="pl-pds">"</span>urlPathHelper<span class="pl-pds">"</span></span>&gt;
    &lt;<span class="pl-ent">bean</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>com.yoochoose.frontend.spring.UrlPathHelperFixed<span class="pl-pds">"</span></span>/&gt;
  &lt;/<span class="pl-ent">property</span>&gt;
&lt;/<span class="pl-ent">bean</span>&gt;</pre></div>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.2.5</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398109888" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12574" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12574/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12574">#12574</a> Request mapping using <code class="notranslate">@PathVariable</code> doesn't work with URL-encoded values</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398210584" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/20202" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/20202/hovercard" href="https://github.com/spring-projects/spring-framework/issues/20202">#20202</a> [doc] Document urlDecode=false implies alwaysUseFullPath=true</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/12598f85811c61dbe2a205826274ec4dd6a85961/hovercard" href="https://github.com/spring-projects/spring-framework/commit/12598f85811c61dbe2a205826274ec4dd6a85961"><tt>12598f8</tt></a>, <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/d55a173e2e829baaa727daffc839fa93c1663ab5/hovercard" href="https://github.com/spring-projects/spring-framework/commit/d55a173e2e829baaa727daffc839fa93c1663ab5"><tt>d55a173</tt></a></p>
<p dir="auto"><strong>Backported to:</strong> <a href="https://github.com/spring-projects/spring-framework/milestone/96?closed=1">3.2.8</a></p>
<p dir="auto">3 votes, 10 watchers</p>