<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=eric.bottard" rel="nofollow">Eric Bottard</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5924?redirect=false" rel="nofollow">SPR-5924</a></strong> and commented</p>
<p dir="auto">Hard coded request mapping values should take precedence over path variables.  Wild card patterns in a path are currently inferior to explicit values.  Path variables should be applied after explicit paths and before wild cards.</p>
<p dir="auto">For example:</p>
<p dir="auto"><code class="notranslate">@RequestMapping</code>(value = "/resources/new/", method = RequestMethod.GET)<br>
is currently trumped by<br>
<code class="notranslate">@RequestMapping</code>(value = "/resources/{resourceName}/", method = RequestMethod.GET)</p>
<p dir="auto"><code class="notranslate">@RequestMapping</code>(value = "/resources/new/", method = RequestMethod.GET)<br>
currently trumps<br>
<code class="notranslate">@RequestMapping</code>(value = "/resources/*/", method = RequestMethod.GET)</p>
<p dir="auto"><code class="notranslate">@RequestMapping</code>(value = "/resources/new/", method = RequestMethod.GET)<br>
should trump<br>
<code class="notranslate">@RequestMapping</code>(value = "/resources/{resourceName}/", method = RequestMethod.GET)<br>
should trump<br>
<code class="notranslate">@RequestMapping</code>(value = "/resources/*/", method = RequestMethod.GET)</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0 M1</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="398092403" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10035" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10035/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10035">#10035</a> <code class="notranslate">@RequestMapping</code> best match gives wrong Controller method when using wildcards (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398092440" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10040" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10040/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10040">#10040</a> PathVariable mappings are greedy over hard coded mappings</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398094868" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10401" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10401/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10401">#10401</a> <code class="notranslate">@Controller</code> method order effects <code class="notranslate">@RequestMapping</code> behavior in ways not expected</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/8073efd69f481590a6d22688dc2b9a0e8f6e124e/hovercard" href="https://github.com/spring-projects/spring-framework/commit/8073efd69f481590a6d22688dc2b9a0e8f6e124e"><tt>8073efd</tt></a>, <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/abfc479bdb0eb28decc24fb720bb4e6827cbe7e3/hovercard" href="https://github.com/spring-projects/spring-framework/commit/abfc479bdb0eb28decc24fb720bb4e6827cbe7e3"><tt>abfc479</tt></a></p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=jon_ruckwood" rel="nofollow">Jonathan Ruckwood</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7242?redirect=false" rel="nofollow">SPR-7242</a></strong> and commented</p>
<p dir="auto">I'm in a situation where there is prolific use of <code class="notranslate">@ModelAttribute</code> annotations in a project, however in most circumstances the model-data is only needed on either the POST or GET, consequently we are needlessly resolving attributes when they won't be used.</p>
<p dir="auto">I'm proposing something along the lines of this:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@ModelAttribute(&quot;orderAddressList&quot;, RequestMethod.GET)
@ModelAttribute(&quot;orderAddressList&quot;, {RequestMethod.GET, RequestMethod.POST})
@ModelAttribute(&quot;orderAddressList&quot;) // Optional, without any RequestMethod the annotation works as it does currently."><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">ModelAttribute</span>(<span class="pl-s">"orderAddressList"</span>, <span class="pl-smi">RequestMethod</span>.<span class="pl-c1">GET</span>)
<span class="pl-c1">@</span><span class="pl-c1">ModelAttribute</span>(<span class="pl-s">"orderAddressList"</span>, {<span class="pl-smi">RequestMethod</span>.<span class="pl-c1">GET</span>, <span class="pl-smi">RequestMethod</span>.<span class="pl-c1">POST</span>})
<span class="pl-c1">@</span><span class="pl-c1">ModelAttribute</span>(<span class="pl-s">"orderAddressList"</span>) <span class="pl-c">// Optional, without any RequestMethod the annotation works as it does currently.</span></pre></div>
<p dir="auto">The following should not be allowed, i.e. an exception should be thrown:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@ModelAttribute(&quot;foo&quot;, RequestMethod.GET)
public Foos getFoos() {
	return foos;
}

@RequestMapping(method = RequestMethod.POST)
public String doPost(@ModelAttribute(&quot;foo&quot;) Foos foos) { // &lt;-- causes exception as foo is only available for GET request.
	return &quot;fooView&quot;;
}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">ModelAttribute</span>(<span class="pl-s">"foo"</span>, <span class="pl-smi">RequestMethod</span>.<span class="pl-c1">GET</span>)
<span class="pl-k">public</span> <span class="pl-smi">Foos</span> <span class="pl-s1">getFoos</span>() {
	<span class="pl-k">return</span> <span class="pl-s1">foos</span>;
}

<span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s1">method</span> = <span class="pl-smi">RequestMethod</span>.<span class="pl-c1">POST</span>)
<span class="pl-k">public</span> <span class="pl-smi">String</span> <span class="pl-s1">doPost</span>(<span class="pl-c1">@</span><span class="pl-c1">ModelAttribute</span>(<span class="pl-s">"foo"</span>) <span class="pl-smi">Foos</span> <span class="pl-s1">foos</span>) { <span class="pl-c">// &lt;-- causes exception as foo is only available for GET request.</span>
	<span class="pl-k">return</span> <span class="pl-s">"fooView"</span>;
}</pre></div>
<p dir="auto">An added benefit of this is that it increases readability of annotated controllers. For example, a new developer can get more information from a controller's data-model by scanning the annotations alone.</p>
<p dir="auto">Implementing this would help us mitigate our problem, however I also feel that it would be a useful enhancement to the wider community.</p>
<p dir="auto">Thanks.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.2</p>
<p dir="auto">4 votes, 7 watchers</p>