title: Java based configuration of Spring DispatcherServlet
body: <p>I have a task to make my JAR application a Spring MVC application. I installed Tomcat 9.0.5 and proceeded to configure spring. For my sample test application, I decided to go with 100% Java configuration of DispatcherServlet and no web.xml as instructed here: <a href="https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/web.html#mvc-servlet" rel="nofollow noreferrer">https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/web.html#mvc-servlet</a> but I can't make it work.</p>
  
  <p>I created a project using IntelliJ. It downloaded spring jars and created a directory structure like that:</p>
  
  <pre><code>tomcattest
  |-.idea
  |-lib
  |-out
  |-src
  |-web
  ||-index.jsp
  |-tomcattest.iml
  </code></pre>
  
  <p>I run this empty application under my Tomcat and index.jsp has been loaded.</p>
  
  <p>Then I created <code>MyWebApplicationInitializer</code>:</p>
  
  <pre><code>package pl.mesayah.tomcattest;
  
  import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  
  public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return null;
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[]{AppConfig.class};
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[]{"/"};
      }
  }
  </code></pre>
  
  <p><code>AppConfig</code>:</p>
  
  <pre><code>package pl.mesayah.tomcattest;
  
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.EnableAspectJAutoProxy;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @Configuration
  @ComponentScan("pl.mesayah.tomcattest")
  @EnableAspectJAutoProxy
  @EnableWebMvc
  public class AppConfig extends WebMvcConfigurerAdapter {
  
  }
  </code></pre>
  
  <p>And sample <code>@RestController</code>:</p>
  
  <pre><code>package pl.mesayah.tomcattest;
  
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class TestController {
  
      @GetMapping("/test")
      public String test() {
          return "test";
      }
  }
  </code></pre>
  
  <p>But I can't make it work. After deploying to Tomcat I can still see index.jsp page but going to <a href="http://localhost:8080/test" rel="nofollow noreferrer">http://localhost:8080/test</a> gives 404 error page and It's Tomcat's 404, not Spring's. So I thought that DispatcherServlet isn't working at all, but I have no idea how to do it. I was following official Spring documentation from the link above.</p>
  
  <p>Please, tell me what I am missing in my code.</p>