title: Spring MVC: Ambiguous mapping found with multiple Dispatchers
body: <p>i'm building a WebApplication with Spring4. The WebApplication Dispatcher is mapped to /app/* because i have a second Spring Dispatcher for REST Services with mapping /services/*</p>
  
  <p>When i try to start the WebApplication Spring throws Exceptions (Ambiguous mapping found) because i have the same mapping ("/persons") in two different Controllers.
  It is correct, i have this mapping in a Controller in my WebApplication and the same mapping in a RestController. But the Controllers are in different Dispatchers with the different dispatcher mappings.
  Is there a way to explain Spring that this mappings are correct?
  Or is it a mistake from myself and the way i go is complete wrong?</p>
  
  <p>Output from Jetty (Stripped some unneeded things):</p>
  
  <pre><code>INFO: Root WebApplicationContext: initialization started
  INFO: Mapped "{[/home],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.Home.showMessage()
  INFO: Mapped "{[/home],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.Home.showMessage()
  INFO: Mapped "{[/persons],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.PersonController.list()
  INFO: Mapped "{[/persons],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.PersonController.save(io.theoperator.model.Person)
  INFO: Root WebApplicationContext: initialization completed in 1319 ms
  INFO: FrameworkServlet 'serviceapplication': initialization started
  INFO: Mapped "{[/persons/page],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map&lt;java.lang.String, java.lang.Object&gt; io.theoperator.restservice.PersonServiceController.getPage(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
  INFO: Mapped "{[/persons],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public io.theoperator.restservice.PersonServiceController$PersonList io.theoperator.restservice.PersonServiceController.getAll()
  INFO: Mapped "{[/persons/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public io.theoperator.model.Person io.theoperator.restservice.PersonServiceController.getPerson(java.lang.String)
  Mar 01, 2015 12:36:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
  INFO: Mapped "{[/persons/page],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map&lt;java.lang.String, java.lang.Object&gt; io.theoperator.restservice.PersonServiceController.getPage(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
  Mar 01, 2015 12:36:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
  INFO: Mapped "{[/persons],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public io.theoperator.restservice.PersonServiceController$PersonList io.theoperator.restservice.PersonServiceController.getAll()
  Mar 01, 2015 12:36:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
  INFO: Mapped "{[/persons/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public io.theoperator.model.Person io.theoperator.restservice.PersonServiceController.getPerson(java.lang.String)
  Mar 01, 2015 12:36:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
  INFO: Mapped "{[/home],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.Home.showMessage()
  Mar 01, 2015 12:36:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
  INFO: Mapped "{[/persons/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView io.theoperator.controller.PersonController.details(java.lang.String)
  Mar 01, 2015 12:36:03 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext refresh
  WARNING: Exception encountered during context initialization - cancelling refresh attempt
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method 
  public org.springframework.web.servlet.ModelAndView io.theoperator.controller.PersonController.list()
  to {[/persons],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personServiceController' bean method
  public io.theoperator.restservice.PersonServiceController$PersonList io.theoperator.restservice.PersonServiceController.getAll() mapped.
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      at javax.servlet.GenericServlet.init(GenericServlet.java:244)
      at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:613)
      at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:396)
      at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:871)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:341)
      at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1343)
      at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp(JettyWebAppContext.java:296)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1336)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:742)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499)
      at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:365)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
      at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
      at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:163)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
      at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
      at org.eclipse.jetty.server.Server.start(Server.java:399)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
      at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
      at org.eclipse.jetty.server.Server.doStart(Server.java:366)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:516)
      at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:359)
      at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:167)
      at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
      at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
      at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
      at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
      at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
      at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
      at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
      at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
      at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
      at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>
  
  <p>This is my WebApplicationInitializer for the WebApplication:</p>
  
  <pre><code>public class WebApplicationInitializer implements org.springframework.web.WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext container) {
  
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebApplicationConfiguration.class);
  
      container.addListener(new ContextLoaderListener(rootContext));
  
      ServletRegistration.Dynamic dispatcher = container.addServlet("webapplication", new DispatcherServlet(rootContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/app/*");
  
  }
  }
  </code></pre>
  
  <p>and this is my WebApplicationInitializer for REST Services</p>
  
  <pre><code>public class ServiceApplicationInitializer implements org.springframework.web.WebApplicationInitializer {
  
  @Override
  public void onStartup(ServletContext container) {
  
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(ServiceApplicationConfiguration.class);
  
      ServletRegistration.Dynamic dispatcher = container.addServlet("serviceapplication", new DispatcherServlet(rootContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/services/*");
  
  }
  }
  </code></pre>
  
  <p>Here are the Configurations for the Dispatchers:</p>
  
  <pre><code>@EnableWebMvc
  @ComponentScan(basePackages = {
          "io.theoperator.controller",
          "io.theoperator.service",
          "io.theoperator.repository",
          "io.theoperator.configuration",
  })
  @Configuration
  public class WebApplicationConfiguration extends WebMvcConfigurerAdapter {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
      }
  
      @Bean
      public InternalResourceViewResolver getInternalResourceViewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("/WEB-INF/pages/");
          resolver.setSuffix(".jsp");
          return resolver;
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@EnableWebMvc
  @ComponentScan(basePackages = {
          "io.theoperator.service",
          "io.theoperator.repository",
          "io.theoperator.configuration",
          "io.theoperator.restservice"
  })
  public class ServiceApplicationConfiguration extends WebMvcConfigurerAdapter {
  
  
  
  }
  </code></pre>
  
  <p>In my WebApplication i have this Controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/persons")
  public class PersonController {
  
      @RequestMapping(method = RequestMethod.GET)
      public ModelAndView list() {
          return new ModelAndView("persons/list");
      }
  }
  </code></pre>
  
  <p>In my ServiceApplication i have a RestController:</p>
  
  <pre><code>@RestController
  @RequestMapping("/persons")
  public class PersonServiceController {
  @RequestMapping(method = RequestMethod.GET)
      public PersonList getAll() {
          return new PersonList(this.personService.list());
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I have removed the ContextLoaderListener how magnama suggested. But the error is effectively the same.</p>
  
  <p><a href="http://pastebin.com/DmYgZTAN" rel="nofollow">Here</a> (Pastebin) is the complete output from Spring. I think something goes very wrong. Spring starts at first the serviceapplication context and registers /home (From HomeController) that is part of the WebApplication. The HomeController is in package io.theoperator.controller which is <em>not</em> part of the ComponentScan of the ServiceApplicationConfiguration...
  At the moment i have no idea what is wrong...</p>