<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=jack_kerouac" rel="nofollow">Florian Rampp</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7288?redirect=false" rel="nofollow">SPR-7288</a></strong> and commented</p>
<p dir="auto">In the given forum entry, I outline a use case for splitting the handling of a REST resource between several controllers. While, e.g. the methods PUT and DELETE on a resource describe the setup and teardown of some kind of bucket, the POST to the same URL creates new content in the bucket. These two kinds of functionality are very different and thus may have two different business service interfaces. Therefore a split in the controller to reflect this distinction would be reasonable.</p>
<p dir="auto">I would propose to consider, whether the rule that one resource can only handled by a single controller should be extended to include the HTTP method, so that different HTTP methods of the same resource can be handled in different controllers.</p>
<hr>
<p dir="auto"><strong>Reference URL:</strong> <a href="http://forum.springsource.org/showthread.php?t=90822" rel="nofollow">http://forum.springsource.org/showthread.php?t=90822</a></p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=markhopkins" rel="nofollow">Mark Hopkins</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-6822?redirect=false" rel="nofollow">SPR-6822</a></strong> and commented</p>
<p dir="auto">Given the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Controller
public class Controller1 {
  @RequestMapping(value=&quot;/someurl&quot;, method=GET)
  public String doGet() {
    return &quot;view&quot;;	
  }	
}"><pre class="notranslate"><code class="notranslate">@Controller
public class Controller1 {
  @RequestMapping(value="/someurl", method=GET)
  public String doGet() {
    return "view";	
  }	
}
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Controller
public class Controller2 {
  @RequestMapping(value=&quot;/someurl&quot;, method=POST)
  public String doPost() {
    // do something different for posts	
    return &quot;view&quot;;	
  }	
}"><pre class="notranslate"><code class="notranslate">@Controller
public class Controller2 {
  @RequestMapping(value="/someurl", method=POST)
  public String doPost() {
    // do something different for posts	
    return "view";	
  }	
}
</code></pre></div>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="....
    &lt;context:spring-configured/&gt;
    &lt;context:annotation-config/&gt;
    &lt;context:component-scan base-package=&quot;...&quot;/&gt;
    &lt;context:component-scan base-package=&quot;...&quot;/&gt;
...."><pre class="notranslate">....
    &lt;<span class="pl-ent">context</span><span class="pl-ent">:</span><span class="pl-ent">spring-configured</span>/&gt;
    &lt;<span class="pl-ent">context</span><span class="pl-ent">:</span><span class="pl-ent">annotation-config</span>/&gt;
    &lt;<span class="pl-ent">context</span><span class="pl-ent">:</span><span class="pl-ent">component-scan</span> <span class="pl-e">base-package</span>=<span class="pl-s"><span class="pl-pds">"</span>...<span class="pl-pds">"</span></span>/&gt;
    &lt;<span class="pl-ent">context</span><span class="pl-ent">:</span><span class="pl-ent">component-scan</span> <span class="pl-e">base-package</span>=<span class="pl-s"><span class="pl-pds">"</span>...<span class="pl-pds">"</span></span>/&gt;
....</pre></div>
<p dir="auto">you get the following exception:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/web-application-config.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler [controller2] to URL path [/someurl]: There is already handler [Controller1@1830ec7] mapped.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
..."><pre class="notranslate"><code class="notranslate">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/web-application-config.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler [controller2] to URL path [/someurl]: There is already handler [Controller1@1830ec7] mapped.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
...
</code></pre></div>
<p dir="auto">In our specific domain it makes most sense to have the GET and POST for the same URL split over two different classes.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0 GA</p>
<p dir="auto">2 votes, 3 watchers</p>