title: Set a Spring REST Controller welcome-file
body: <p>I'm building a RESTful API and have a Spring REST Controller (@RestController) and an annotation-based configuration. I'd like to have my project's welcome-file be a .html or .jsp file with the API documentation. </p>
  
  <p>In other web projects I would place a welcome-file-list in my web.xml, but in this particular project I can't seem to get it to work (preferrably using Java and annotations).</p>
  
  <p>This is my WebApplicationInitializer</p>
  
  <pre><code>public class WebAppInitializer implements WebApplicationInitializer {
  
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
          context.register(ApplicationConfig.class);
          context.setServletContext(servletContext);
  
          ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher", 
             new DispatcherServlet(context));
          dynamic.addMapping("/");
          dynamic.setLoadOnStartup(1);
      }
  }
  </code></pre>
  
  <p>This is my WebMvcConfigurerAdapter</p>
  
  <pre><code>@Configuration
  @ComponentScan("controller")
  @EnableWebMvc
  public class ApplicationConfig extends WebMvcConfigurerAdapter {
  
      @Bean
      public Application application() {
          return new Application("Memory");
      }
  
  }
  </code></pre>
  
  <p>And this is a small part of my REST Controller</p>
  
  <pre><code>@RestController
  @RequestMapping("/categories")
  public class CategoryRestController {
  
      @Autowired
      Application application;
  
      @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity&lt;Map&lt;Integer, Category&gt;&gt; getCategories(){
          if(application.getCategories().isEmpty()) {
              return new ResponseEntity&lt;Map&lt;Integer, Category&gt;&gt;(HttpStatus.NO_CONTENT);
          }
          return new ResponseEntity&lt;Map&lt;Integer, Category&gt;&gt;(application.getCategories(), HttpStatus.OK);
      }
  
  }
  </code></pre>
  
  <p>So far I've tried:</p>
  
  <ul>
  <li>Adding just a web.xml with a <code>&lt;welcome-file-list&gt;</code> with a <code>&lt;welcome-file&gt;</code>. (no luck there)</li>
  <li>Moving the <code>@RequestMapping("/categories")</code> in the Controller from the class level to all of the methods, and adding a new method with <code>@RequestMapping("/")</code>, which returns either a <code>String</code> or a <code>ModelAndView</code> with the view name. (the former just returned a blank page with the String, for the latter no mapping could be found)</li>
  <li>As suggested <a href="https://stackoverflow.com/questions/31205519/how-can-i-make-welcome-file-list-work-with-spring-servlet?rq=1">here</a>: a combination of both, where my web.xml <code>&lt;welcome-file&gt;</code> is "/index", combined with <code>@RequestMapping(value="/index")</code> returning a <code>new ModelAndView("index")</code>, and a <code>ViewResolver</code> in my configuration class. (returns a <code>Warning: No mapping found in DispatcherServlet with name 'dispatcher'</code>, even though "/index" is successfully mapped. Manually adding "/index" to the URL successfully resolves it to index.jsp)</li>
  </ul>