1

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.

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).

This is my WebApplicationInitializer

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);
    }
}

This is my WebMvcConfigurerAdapter

@Configuration
@ComponentScan("controller")
@EnableWebMvc
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Bean
    public Application application() {
        return new Application("Memory");
    }

}

And this is a small part of my REST Controller

@RestController
@RequestMapping("/categories")
public class CategoryRestController {

    @Autowired
    Application application;

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<Integer, Category>> getCategories(){
        if(application.getCategories().isEmpty()) {
            return new ResponseEntity<Map<Integer, Category>>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<Map<Integer, Category>>(application.getCategories(), HttpStatus.OK);
    }

}

So far I've tried:

  • Adding just a web.xml with a <welcome-file-list> with a <welcome-file>. (no luck there)
  • Moving the @RequestMapping("/categories") in the Controller from the class level to all of the methods, and adding a new method with @RequestMapping("/"), which returns either a String or a ModelAndView with the view name. (the former just returned a blank page with the String, for the latter no mapping could be found)
  • As suggested here: a combination of both, where my web.xml <welcome-file> is "/index", combined with @RequestMapping(value="/index") returning a new ModelAndView("index"), and a ViewResolver in my configuration class. (returns a Warning: No mapping found in DispatcherServlet with name 'dispatcher', even though "/index" is successfully mapped. Manually adding "/index" to the URL successfully resolves it to index.jsp)
030
  • 10,842
  • 12
  • 78
  • 123
Joris
  • 309
  • 2
  • 12
  • For the index controller don't use a `@RestController` but a `@Controller`. Else you get the behavior your describe *just returned a blank page with the String*... – M. Deinum May 01 '16 at 07:08
  • https://stackoverflow.com/questions/24670327/welcome-file-not-working-with-html-file-in-spring – OhadR Jan 03 '19 at 14:05

1 Answers1

1

When specifying a controller to handle your index page you should use a @Controller not a @RestController. Although the @RestController is a @Controller it doesn't resolve to a view but returns the result as is to the client. When using a @Controller when returning a String it will resolve to the name of a view.

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

However there is an easier way to configure this and you don't need a controller for it. Configure a view controller. In your configuration class simply override/implement the addViewControllers method.

public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
}

That way you don't even need to create a class for it.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • With this configuration, how can I access my stylesheet outside WEB-INF from my JSP inside WEB-INF? I've tried `../css/stylesheet.css` as well as `${pageContext.request.contextPath}/css/stylesheet.css`, both with and without `c:url` tag but to no avail. – Joris May 01 '16 at 19:20
  • Apologies, I just found the answer here: http://stackoverflow.com/questions/14861720/annotation-configuration-replacement-for-mvcresources-spring – Joris May 01 '16 at 19:24