title: Spring 4 REST application using Java configuration (no xml) IllegalArgumentException
body: <p>This is driving me crazy. I've done a good bit of Spring coding but this is the first time that I'm trying to do all configuration in Java. I've searched all over stackoverflow and other places, and while others have seen this problem, none of their solutions have worked for me.</p>
  
  <p>Here are my source files:</p>
  
  <p>Initializer:</p>
  
  <pre><code>public class EnhancedCandidateInfoInitializer extends  AbstractAnnotationConfigDispatcherServletInitializer {
      private static final Logger logger = LoggerFactory.getLogger(EnhancedCandidateInfoInitializer.class);
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          logger.info("@@@@@ getRootConfigClasses called - returning null @@@@@");
          return null;
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          logger.info("@@@@@ getServletConfigClasses called returning EnhancedCandidateInfoWebConfiguration.class @@@@@");
          return new Class[] { EnhancedCandidateInfoWebConfiguration.class    };
      }
  
      @Override
      protected String[] getServletMappings() {
          logger.info("@@@@@ getServletMappings called @@@@@");
          return new String[] { "/" };
      }
  }
  </code></pre>
  
  <p>WebMvcConfiguration:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = {"com.mojorank.restapi"})
  public class EnhancedCandidateInfoWebConfiguration extends WebMvcConfigurerAdapter {
      private static final Logger logger =  LoggerFactory.getLogger(EnhancedCandidateInfoWebConfiguration.class);
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
          logger.info("#### My Configuration handler was called ####");
          configurer.enable();
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@RestController
  public class EnhanceCandidateInfoController {
      @RequestMapping("/")
      public String welcome() {//Welcome page, non-rest
          return "Welcome to RestTemplate Example.";
      }
  
      @RequestMapping("/hello/{player}")
      public Message message(@PathVariable String player) {//REST Endpoint.
  
          Message msg = new Message(player, "Hello " + player);
          return msg;
      }
  }
  </code></pre>
  
  <p>When I build and deploy the application to tomcat, I get the following exception stack trace:</p>
  
  <blockquote>
    <p>Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name.
           at org.springframework.util.Assert.notNull(Assert.java:115)
           at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98)
           at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:71)
           at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
           at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5274)
           at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)</p>
  </blockquote>
  
  <p>As I said, I've searched through stackoverflow and other places and found others that had my same problem, but when I tried to implement the proposed fixes, my problem remained. Thanks in advance for any help.</p>