title: Understanding How Spring MVC's @RequestMapping POST Works
body: <p>I have a simple Controller that looks like this:-</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/groups")
  public class GroupsController {
      // mapping #1
      @RequestMapping(method = RequestMethod.GET)
      public String main(@ModelAttribute GroupForm groupForm, Model model) {
          ...
      }
  
      // mapping #2
      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public String changeGroup(@PathVariable Long id, @ModelAttribute GroupForm groupForm, Model model) {
          ...
      }
  
      // mapping #3
      @RequestMapping(method = RequestMethod.POST)
      public String save(@Valid @ModelAttribute GroupForm groupForm, BindingResult bindingResult, Model model) {
          ...
      }
  }
  </code></pre>
  
  <p>Basically, this page has the following functionalities:-</p>
  
  <ul>
  <li>User visits main page (<code>/groups GET</code>).</li>
  <li>User creates a new group (<code>/groups POST</code>) or selects a specific group (<code>/groups/1 GET</code>).</li>
  <li>User edits an existing group (<code>/groups/1 POST</code>).</li>
  </ul>
  
  <p>I understand how both GET request mappings work here. Mapping #2 is defined, otherwise (<code>/groups/1 GET</code>) will cause a "No mapping found" exception.</p>
  
  <p>What I'm trying to understand here is why mapping #3 handles both (<code>/groups POST</code>) and (<code>/groups/1 POST</code>)? It makes sense that it should handle (<code>/groups POST</code>) here since the request mapping matches the URI. Why (<code>/groups/1 POST</code>) isn't causing a "No mapping found" exception being thrown here? In fact, it almost seems like any POST with URI beginning with /groups (ex: <code>/groups/bla/1 POST</code>) will also be handled by mapping #3.</p>
  
  <p>Can someone provide a clear explanation of this to me? Thanks much.</p>
  
  <p><strong>CLARIFICATION</strong></p>
  
  <p>I understand the fact that I can use more appropriate methods (like GET, POST, PUT or DELETE)... or I can create yet another request mapping to handle <code>/groups/{id} POST</code>. </p>
  
  <p>However, what I want to really know is... </p>
  
  <p>.... <strong>"Why does mapping #3 handle <code>/groups/1 POST</code> too?"</strong></p>
  
  <p>The "closest match" reasoning don't seem to hold true because if I remove mapping #2, then I would think mapping #1 will handle <code>/groups/1 GET</code>, but it doesn't and it causes a "No mapping found" exception.</p>
  
  <p>I'm just a little stumped here.</p>